开发者

Can't I assign values from a $().attr() in the same line?

开发者 https://www.devze.com 2023-03-28 02:05 出处:网络
I\'m trying to do this: $(\'#smallpictureone\').click(function () { $(\"mainproductpicture\").attr(\"src\") = $(\"smallpictureone\").attr(\"src\");

I'm trying to do this:

$('#smallpictureone').click(function () {
    $("mainproductpicture").attr("src") = $("smallpictureone").attr("src");
});

#mainproductpicture is the bigger pict开发者_如何学Pythonure 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?


jQuery's attr() function uses the following syntax:

$('yourselector').attr('attribute','value');

For your purposes, using this may also help:

$('#smallpictureone').click(function () {
    $("#mainproductpicture").attr("src", $(this).attr("src"));
});

There was also a # missing the the selector for mainproductpicture:

$("mainproductpicture") should be $("#mainproductpicture");


change:

 $("mainproductpicture").attr("src") = $("smallpictureone").attr("src");

to:

 $("#mainproductpicture").attr("src", $("#smallpictureone").attr("src"));

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


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)...

$('#smallpictureone').click(function () {
    $("#mainproductpicture").attr("src", $("#smallpictureone").attr("src"));
});


First off, you should add the selectors, # or .

$('#smallpictureone').click(function () {
    $("#mainproductpicture").attr("src") = $("#smallpictureone").attr("src");
});

But I guess that was just a typo, or not?

Secondly, it has to be in this form:

attr( attributeName, value )

$('#smallpictureone').click(function () {
    $("#mainproductpicture").attr("src", $(this).attr("src"));
});


Have you tried

$('#smallpictureone').click(function () {
    $("#mainproductpicture").attr("src",$("#smallpictureone").attr("src"));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消