使用 jQuery 变量为链接分配属性

发布于 2025-01-02 02:52:18 字数 351 浏览 3 评论 0原文

我正在尝试使用 jQuery 将属性“href”分配给链接。问题是我的“href”值是另一个对象的“src”值。所以我的最终代码如下所示:

imgLink.attr("href", imgArray.eq(i).attr("src"));

imgLink 是我的“a”标签,imgArray 是“img”数组。所以,在纸面上,我为我的“a”标签分配了一个“href”属性,其值为: imgArray.eq(i).attr("src")... 但没有任何作用,我收到此错误:“imgLink.attr 不是函数”。

有人知道这里发生了什么事吗?谢谢你!

I'm trying to assign an attribute "href" to a link using jQuery. The thing is my "href" value is the "src" value of another object. So my final code looks like this:

imgLink.attr("href", imgArray.eq(i).attr("src"));

imgLink is my "a" tag and imgArray is an array of "img". So I'm, on paper, asigning to my "a" tag an "href" attribute with value: imgArray.eq(i).attr("src")... But nothing works, I get this error: "imgLink.attr is not a function".

Anyone knows what's happening here ? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2025-01-09 02:52:18

很可能 imgLink 不是 jQuery 对象。试试这个。

$(imgLink).attr("href", imgArray.eq(i).attr("src"));

如果 imgArray 也不是 jQuery 对象,则将其也转换为 jQuery 对象。

$(imgLink).attr("href", $(imgArray).eq(i).attr("src"));

Mostly likely imgLink is not a jQuery object. Try this.

$(imgLink).attr("href", imgArray.eq(i).attr("src"));

If imgArray is also not a jQuery object then convert that also into jQuery object.

$(imgLink).attr("href", $(imgArray).eq(i).attr("src"));
檐上三寸雪 2025-01-09 02:52:18

尝试用 jQuery 包装 DOM 元素。

jQuery(imgLink).attr("href", jQuery(imgArray.eq(i)).attr("src"));

Try wrapping your DOM elements in jQuery.

jQuery(imgLink).attr("href", jQuery(imgArray.eq(i)).attr("src"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文