简体   繁体   中英

How to get specific span's value from div in JQuery

I have a div and i want to get text of a specific span from that div. Following is my Div code:

'<div class="dvDynamic_' + pid + '"><p hidden="true">'+pid+'</p><span class="count_' + pid + '">' + count + '</span><span id="pname" style = "margin-left:70px;">' + pname + '</span><span id="punitprice" style = "margin-left:150px;">' + uprice + '</span></div>' 

and i want to get text of the following span:

<span class="count_' + pid + '">' + count + '</span>

Please help me how to do it .

The span element which you want to target have next sibling with id. you can target that element using id selector and traverse to required span using .prev() :

$('#pname').prev().text()

如果您知道要获取的跨度的pid ,则可以使用

$('.count_' + pid).text()

您可以使用ID选择器识别pname元素,然后使用.prev()识别所需的span

$('#pname').prev().text()

Using find you can get value of span through div

function findSpanValue(pid){
    return $(".dvDynamic_"+pid).find(".count_"+pid).text();
}

Get span element to target have next sibling with id. you can get that element using id selector and traverse to required span using .prev():

$('#pname').prev().html();

or

$('#pname').prev().text();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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