我正在尝试for使用jQuery 获取label元素的属性.到目前为止,我的所有尝试都产生了不正确的反应.
console.log($('label'.htmlFor)); //e.fn.init.....
console.log($('label').htmlFor)); //undefined
console.log($('label.htmlFor')); //[prevObject.....]..
console.log($('label[for]'));
console.log($('label')); //same as directly above
console.log($('label[for].htmlFor')); //[prevObject.....]..
console.log($('label[for]').htmlFor)); //undefined
Run Code Online (Sandbox Code Playgroud)
我知道htmlFor定义为我可以在chromes web检查器中看到它.我究竟做错了什么?
您的示例不起作用,因为您要么仅选择元素(并且不检索属性值),要么尝试获取htmlFor不是jQuery对象的有效属性的属性.
要做你需要的,你需要选择label然后使用attr()来获取for属性,如下所示:
var forAttr = $('label[for]').attr('for');
Run Code Online (Sandbox Code Playgroud)
或者,如果label表单中有多个元素,您可以像这样遍历它们:
$('label[for]').each(function() {
var forAttr = $(this).attr('for');
// work with for here...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |