2015-05-05 94 views
0

我想獲取此元素的內部html。如何使用jquery獲取標籤元素的內部html文本

<label for="ctl00_ctl00_MainContent_ContentPlaceHolder_chkUsers_0">DOE, RICHARD A(033495)</label> 

但是,我的jQuery返回undefined爲customerLabel

var customerLabelId = $('label:contains("ContentPlaceHolder_chkUsers_0")').attr("for"); 
var customerLabel = $('#' + customerLabelId); 
var customerValueInInnerHtml = customerLabel.val(); 

回答

2

您需要使用.filter()來獲取在屬性有ContentPlaceHolder_chkUsers_0的元素。

另外,.val()用於獲取表單元素的值,如input,select,textarea。你可能會需要使用.html().text()獲得標籤內容:

$('label').filter(function(){ 
    return $(this).attr('for').indexOf("ContentPlaceHolder_chkUsers_0") > -1; 
}).html(); 

Working Demo

1

使用 「文本()」 如果你想獲得純文本。

var customerValueInInnerHtml = customerLabel.text(); 

使用 「的.html()」 如果你想與所有的HTML標籤內的HTML:

var customerValueInInnerHtml = customerLabel.html(); 

使用 「.VAL()」 如果你想獲得像輸入元素的值,文本區域,請選擇

相關問題