jQuery get value within child div

后端 未结 8 1696
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 01:50

I need to grab the text value of a child div.

A
B
8条回答
  •  长发绾君心
    2021-02-04 02:29

    You want to use children() and text() instead of val(). Although, since what you are selecting has an id (and ids must be unique), you could also simply select based on the id without involving the container element at all.

    The val() method only works on input elements, textareas, and selects -- basically all form elements that contain data. To get the textual contents of a container, you need to use text() (or html(), if you want the mark up as well).

    var text_val = $('#second_child').text(); //preferred
    

    or

    var text_val = $('#first').children('#second_child').text(); // yours, corrected 
    

提交回复
热议问题