我不能在同一行中从 $().attr() 分配值吗?
我正在尝试这样做:
$('#smallpictureone').click(function () {
$("mainproductpicture").attr("src") = $("smallpictureone").attr("src");
});
#mainproductpicture
是我页面上的更大图片。因此,当有人点击较小的图片时,应该将较小的图片的 src 设置为较大的图片。
我的代码有问题吗?
I'm trying to do this:
$('#smallpictureone').click(function () {
$("mainproductpicture").attr("src") = $("smallpictureone").attr("src");
});
#mainproductpicture
is the bigger picture on my page. So when someone clicks on the smaller one, the src of that smaller one should be set to the bigger picture.
Is something wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
jQuery 的 attr() 函数使用以下语法:
对于您的出于目的,使用
this
也可能有所帮助:还有一个 # 缺少 mainproductpicture 的选择器:
$("mainproductpicture")
应该是$("#mainproductpicture");
jQuery's attr() function uses the following syntax:
For your purposes, using
this
may also help:There was also a # missing the the selector for mainproductpicture:
$("mainproductpicture")
should be$("#mainproductpicture");
更改:
为:
jQuery attr 采用可选的第二个参数作为值来设置该属性。看来您忘记在事件处理程序 ID 中添加
#
change:
to:
jQuery attr takes an optional second parameter for the value to set that attribute. And it seems you forgot to add your
#
in the event handler IDs您需要将该值作为第二个参数传递给 attr 方法。您还需要使用#符号(假设它们是id)...
You need to pass the value as the second parameter to the attr method. You also need to use the # symbol (assuming they are ids)...
首先,您应该添加选择器 # 或 .
但我想这只是一个错字,或者不是?
其次,它必须采用以下形式:
attr( attributeName, value )
First off, you should add the selectors, # or .
But I guess that was just a typo, or not?
Secondly, it has to be in this form:
attr( attributeName, value )
你尝试过吗
Have you tried