简体   繁体   English

在jQuery中更改图像?

[英]changing the image in jquery?

i have this jquery code which works fine, but the image at the end is not changing to the src i specified here: 我有这个jquery代码,可以正常工作,但是最后的图像没有更改为我在此处指定的src:

jquery: jQuery的:

    $(document).ready( function() {
      $("a.vote_up").click(function(){
        //get the id
        var the_id = $(this).attr('id');

        //the main ajax request
        $.ajax( {
          type: "POST",
          data: "action=vote_up&id=" + the_id,
          url: "ajax/votes.php",
          success: function( msg ) {
            $("span.vote_count#"+the_id).html(msg).fadeIn();
// my problem is here 
            $(".vote_up#" + the_id + " img").attr("src", "img/upvoteActive.png");
          }
        } );
      } );
    } );

the html code: html代码:

<a href='#' class='vote_up' id="$id"><img src="img/uparrow.png" /></a>

Don't use class in combination with ID; 不要将类与ID结合使用; it is redundant because ID should always be unique... 这是多余的,因为ID应该始终是唯一的...

 $("#" + the_id + " img").attr("src", "img/upvoteActive.png");

Also, you cannot use the character $ in the ID attribute. 另外,您不能在ID属性中使用字符$。 To quote the W3C on the ID attribute ... 在ID属性上引用W3C ...

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。

It looks like you are using the same ID multiple times (img and count). 好像您多次使用相同的ID(img和count)。 Try making the ID more unique like: 尝试使ID更独特,例如:

<a href='#' class='vote_up' id="$id_link"><img src="img/uparrow.png" /></a>
<span class="vote_count" id="$id_count"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM