开发者

jQuery button click change image

开发者 https://www.devze.com 2023-02-07 06:01 出处:网络
$(\"#button\").click(function () { //debugger; if ($(\"img[id=image][src:contains(Grass)]\")) $(\"img\").attr({ src: \"/Content/images/Spring.jpg\", alt: \"Spring\" });
$("#button").click(function () {
    //debugger;
    if ($("img[id=image][src:contains(Grass)]"))
        $("img").attr({ src: "/Content/images/Spring.jpg", alt: "Spring" });
    else
        $("img").attr({ src: "/Content/images/Grass.jpg", alt: "Grass" });
});

<a href="/Content/images/Grass.jpg"><img src="/Content/开发者_JAVA百科images/Grass.jpg" alt="image"    id="image"/></a>
<input type="button" id="button" name="button"/>

I have simple form with image and button, i want on each button click image getting changed. The logic is very simple. By default image src = /Content/images/Grass.jpg When i click button first time image getting changed, but when i am click second time it is does not changed back. I check in debugger and find out that condition $("img[id=image][src:contains(Grass)]") always true. But after first button click shouldn't it became false then?

if i declare

    var img1 = $("img#image[src:contains(Grass)]");
    var img2 = $("img#image[src:contains(Grass2)]");
    var img3 = $("img#image[src:contains(blabla)]");

each img1.length = 1 img2.length = 1 img3.length = 1 Why?

but

var img1 = $("img#image[src*='Grass']");
var img2 = $("img#image[src*='Grass2']");
var img3 = $("img#image[src*='blabla']");

each img1.length = 1 img2.length = 0 img3.length = 0 it is what was expected.

Does it means that src:contains(text) and src*=text is so different?


$("#button").click(function () {
  var alt = ($('img#image[alt="Grass"]').length) ? 'Spring' : 'Grass';
  $('img#image').attr({ src: "/Content/images/"+alt+".jpg", alt:alt });
});

<a href="/Content/images/Grass.jpg">
  <img src="/Content/images/Grass.jpg" alt="Grass" id="image"/>
</a>
<input type="button" id="button" name="button"/>


The line if ($("img[id=image][src:contains(Grass)]")) will always be true. The $() function returns a jQuery object, and an object always evaluate to true. You need to check that the length of the result is not 0.

if ($("img[id=image][src:contains(Grass)]").length > 0)


Try this:

var img = $("#image"),
    a = img.parent('a');
$("#button").click(function () {
    var str = (img.attr('src').search('Grass') != -1) ? "Spring" : "Grass",
        src =  "/Content/images/"+str+".jpg";
    img.attr({ src: src, alt: str });
    a.attr("href", src);
});

First we are caching the #image jQuery object, no need to select it each time the button is pressed. Then we do a normal javascript search on the src string. If it has grass in it then we change it to spring. Because we cached the #image jQuery object we don't have to reselect it to change the src and alt attributes. Also if you are changing the img src I am guessing you probably want to update the link around it.


One simple fix is to replace this:

$("img[id=image][src:contains(Grass)]")

...with this:

$("#image[src*=Grass]").length

Related selectors:

a^=b    a starts with b
a$=b    a ends with b
a!=b    a does not equal b
a*=b    a contains b


          $("#button").click(function () {                
            var value = ($("img#image[src*='Grass']").length) ? 'Spring' : 'Grass';

            $("img").attr({ src: "/Content/images/" + value + ".jpg", alt: value });
          });
0

精彩评论

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