Javascript jQuery(此)目标未找到元素
这是怎么回事:Javascript jQuery(此)目标未找到元素,javascript,jquery,Javascript,Jquery,这是怎么回事: jQuery(this).closest("li").find("p").text(); 但当我把它放在函数中时,它就不起作用了: jQuery.each(words, function(i, v) { jQuery(this).closest("li").find("p").text(); }); 我需要通过函数传递这个吗 完整代码: jQuery(document).ready(function() { jQuery('.js-trigger-editb'
jQuery(this).closest("li").find("p").text();
但当我把它放在函数中时,它就不起作用了:
jQuery.each(words, function(i, v) {
jQuery(this).closest("li").find("p").text();
});
我需要通过函数传递这个吗
完整代码:
jQuery(document).ready(function() {
jQuery('.js-trigger-editb').bind("mouseup", function() {
// find the p tag that contains the content and split it
var words = jQuery(this).closest("li").find("p").text().split(" ");
// wrap words in p tag into span tags
jQuery.each(words, function(i, v) {
jQuery(this).closest("li").find("p").append(jQuery("<span>").text(v));
});
});
});
正如你在问题中提到的,函数中的值就是问题所在。在.each的回调中,this的值是正在迭代的数组中的当前项 像这样的方法应该会奏效:
var elem = this; // here, 'this' is the element. Store it.
jQuery.each(words, function(i, v) {
// use the stored element handle from 'elem'
jQuery(elem).closest("li").find("p").append(jQuery("<span>").text(v));
});
正如你在问题中提到的,函数中的值就是问题所在。在.each的回调中,this的值是正在迭代的数组中的当前项 像这样的方法应该会奏效:
var elem = this; // here, 'this' is the element. Store it.
jQuery.each(words, function(i, v) {
// use the stored element handle from 'elem'
jQuery(elem).closest("li").find("p").append(jQuery("<span>").text(v));
});
jQuerythis是对调用的DOM元素的引用。因此,当您在jQuery'.js trigger editb'.bindmouseup,函数{}中调用它时,它是不同的元素;和jQuery.eachwords,functioni,v{}
尝试将其保存到外部。每个
尝试:
jQuerythis是对调用的DOM元素的引用。因此,当您在jQuery'.js trigger editb'.bindmouseup,函数{}中调用它时,它是不同的元素;和jQuery.eachwords,functioni,v{}
尝试将其保存到外部。每个
尝试:
我建议保存指向找到的p元素的链接,不要每次都搜索它
var p = jQuery(this).closest("li").find("p");
var words = p.text().split(" ");
jQuery.each(words, function(i, v) {
p.append(jQuery("<span>").text(v));
});
我建议保存指向找到的p元素的链接,不要每次都搜索它
var p = jQuery(this).closest("li").find("p");
var words = p.text().split(" ");
jQuery.each(words, function(i, v) {
p.append(jQuery("<span>").text(v));
});
OP包含html来查看那个问题:什么是单词?this=v对于这个:可能是使用完全需要html,就像你说的,函数中的这个值是问题所在。为什么不在循环之前缓存jQuerythis.closestli.findp,然后附加到循环中缓存的var?OP include html来查看这个问题:单词是什么?this=v对于这一点:可能需要使用完整的html,正如您所说,函数内部的值是问题所在。为什么不在循环之前缓存jQuerythis.closestli.findp,然后附加到循环中缓存的var?