jquery selector: reference class inside of where i clicked:

╄→尐↘猪︶ㄣ 提交于 2019-12-18 09:21:48

问题


i have this html:

<div class="title"><img class="arrow" src="rightarrow.gif" />Title</div>

i have this click event:

    $(document).ready(function () {
        $('.title').live('click', function () {

            //NEED SOMETHING HERE TO CHANGE SOURCE                 
            $(".arrow").attr("src", "downarrow.gif");
        });
    });

as you can see i want to change the src attribute of the image. my selector above works, but there are other items on the page with class ="arrow", so i need a way to just select this one instance.


回答1:


Use .find() to constrain the selector to only find elements contained within the element that was clicked (represented by $(this)):

$(document).ready(function() {
    $('.title').live('click', function() {
        $(this).find('.arrow').attr('src', 'downarrow.gif');
    });
});



回答2:


Replace with this:

$(".arrow").attr({"src": "downarrow.gif"});



来源:https://stackoverflow.com/questions/5695804/jquery-selector-reference-class-inside-of-where-i-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!