和平伙计们,我有一小段基于jquery的代码,它抓住所有带有"foo"类及其子代的标签,并为它们分配一些价值,我的问题我怎么能以同样的方式工作但不使用jquery,但原生的javascript
jQuery.each(data.results, function(index, value) {
jQuery(".foo").find("*")
.andSelf()
.contents()
.filter(function(){
return this.nodeType === 3;
})
.filter(function(){
// Only match when contains 'simple string' anywhere in the text
if (value.origin != ""){
return this.nodeValue === (value.origin);
}
})
.each(function(){
this.nodeValue = "assign me";
});});
Run Code Online (Sandbox Code Playgroud)
从技术上讲,它是原生的javascript,jQuery是用javascript编写的,你使用javascript来访问它设置的对象和方法.
所以真正的问题是"你可以不使用JQuery包括吗?"
当然可以,但它可能会比使用jQuery更长,更难维护和更笨拙.