gpt4 book ai didi

javascript - 在调用对象中选择元素父级

转载 作者:行者123 更新时间:2023-11-30 13:25:51 26 4
gpt4 key购买 nike

我有以下 jQuery,它使用 zClip 允许复制到剪贴板。它只适用于一个选择器,但不适用于多个选择器。我正在尝试使用 $(this) 来选择父级,但它会使浏览器崩溃。

$('.copylink').zclip({
path:'ZeroClipboard.swf',
copy: jQuery(this).parent().text()
});

这是在 zClip website 上给出的例子

$('a#copy-description').zclip({
path:'js/ZeroClipboard.swf',
copy:$('p#description').text()
});
// The link with ID "copy-description" will copy
// the text of the paragraph with ID "description"

我的 HTML

我正在尝试使用,需要选择上面html的父元素文本

<ul>
<li>the text i will like to select and pull <a href="" class="copylink">Copy</a></li>
<li>the text i will like to select and pull <a href="" class="copylink">Copy</a></li>
<li>the text i will like to select and pull <a href="" class="copylink">Copy</a></li>
<li>the text i will like to select and pull <a href="" class="copylink">Copy</a></li>
<li>the text i will like to select and pull <a href="" class="copylink">Copy</a></li>
</ul>

我认为我的问题在于将 jQuery 包含在 javascript 对象中并使用 this 来捕获范围外的值。

最佳答案

使用.each() 应用插件:

jQuery('.copylink').each(function() {
jQuery(this).zclip({
path:'ZeroClipboard.swf',
copy: jQuery(this).parent().text()
});
});

现在 this 将是对迭代中当前 .copylink 元素的引用。

关于javascript - 在调用对象中选择元素父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com