JQuery Selector for dynamic id

后端 未结 4 731
你的背包
你的背包 2021-01-06 03:58

I am trying to change the image source in the Jquery



        
相关标签:
4条回答
  • 2021-01-06 04:23

    Either you have access to the ID when the JS is created, or you don't. If you don't then you'll have to find another way to target the item eg: $('.wanted')

    If you do, then put it in: $('#<?php echo $status_id[$num]; ?>')

    0 讨论(0)
  • 2021-01-06 04:33

    When you access $(".wanted"+id) , you are actually trying to access an element with the class name = wanted+id. This is because of the '.' before 'wanted'. Also, you seem to be accessing the <a> tag directly and setting it's src attribute. You need to access the <img> tag. What you could try is this:

    var x=document.getElementById(id);
    $(x).find("img")[0].setAttribute("src","/images/wanted_.png");
    
    0 讨论(0)
  • 2021-01-06 04:33

    ID of the HTML elements should be unique across the page.

    You can try

    //I assume id variable is already assigned the id of the element e.g var id = "<?php echo $status_id[$num] ?>";
    
    $("#"+ id).attr("src", '/images/wanted_.png');
    

    If you really want to select an element that has the given id and also the class wanted then try this:

    $("#"+ id + ".wanted ").attr("src", '/images/wanted_.png');
    
    0 讨论(0)
  • 2021-01-06 04:40

    Give it another class like imgToChange, then use $(".imgToChange")

    0 讨论(0)
提交回复
热议问题