使用 jQuery 变量为链接分配属性
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可能
imgLink
不是 jQuery 对象。试试这个。如果
imgArray
也不是 jQuery 对象,则将其也转换为 jQuery 对象。Mostly likely
imgLink
is not a jQuery object. Try this.If
imgArray
is also not a jQuery object then convert that also into jQuery object.尝试用 jQuery 包装 DOM 元素。
Try wrapping your DOM elements in jQuery.