JQuery get content of span

后端 未结 6 1669
萌比男神i
萌比男神i 2021-02-19 00:29

I\'m trying to get the content of a span when a button is clicked. This is the html:

6条回答
  •  臣服心动
    2021-02-19 00:59

    Depending on how many spans you have but if the span will always come directly after the button then the .next('span') will work. If you plan on placing the button anywhere else within the same parent div then you will need to navigate the dom to find it.

    $('selector').click(function() {
        $(this).parent().find('span');
    )};
    

    Also it may be best to add a targetable class or attribute to the span to target it better incase you do plan on a more complex dom or multiple spans.

    Text Here
    
    $('button.select-location').click(function() {
        var span_val = $(this).parent('div').find('span.clicktarget').html();
    });
    

    Hope this helps

提交回复
热议问题