[英]Get attribute values from html elements with jquery
I'm a bit stuck. 我有点卡住了。 I'd like to get the attribute value
"for"
using a jquery selector. 我想使用jQuery选择器获取属性值
"for"
。 Though I am able to successfully log to the console, how can I get/log just the value for the attribute "for"
on the labels? 尽管我能够成功登录到控制台,但是如何只获取/记录标签上
"for"
属性的值? I've tried console.log(labels['for'])
but that isn't the way to go. 我已经尝试过
console.log(labels['for'])
但这不是要走的路。 Below is the related code. 以下是相关代码。
HTML: HTML:
<div class="inline-group" id="album_set-group">
<h2>Albums</h2>
<div class="inline-related">
<h3><b>Album:</b> <span class="inline_label">#1</span></h3>
<fieldset class="module aligned ">
<div class="form-row field-name">
<div>
<label for="id_album_set-0-name">Name:</label> <input id="id_album_set-0-name" maxlength="100" name="album_set-0-name" type="text">
</div>
</div>
<div class="form-row field-release_date">
<div class="date-field">
<label for="id_album_set-0-release_date">Release date:</label> <input id="id_album_set-0-release_date" name="album_set-0-release_date" type="text">
</div>
</div>
</fieldset>
</div>
</div>
JAVASCRIPT: JAVASCRIPT:
$(document).ready(function() {
var labels = $( "#album_set-group" ).find( "label" );
console.log(labels);
});
Please read http://api.jquery.com/attr/ for get attr 请阅读http://api.jquery.com/attr/获取属性
try this code: 试试这个代码:
$(document).ready(function() {
var labels = $("#album_set-group").find("label");
labels.each(function() {
console.log($(this).attr('for'));
});
});
Result: https://jsfiddle.net/cmedina/xo67uqc7/ 结果: https : //jsfiddle.net/cmedina/xo67uqc7/
You can use just the method .attr(). 您可以只使用.attr()方法。 In your case you can do something like
在您的情况下,您可以执行以下操作
$("#album_set-group").find("label").each(function() {
console.log($(this).attr('for'));
});
Plain JavaScript: 纯JavaScript:
<label>
s into a NodeList with querySelectorAll()
querySelectorAll()
将<label>
收集到NodeList中 Array.prototype.slice.call()
Array.prototype.slice.call()
将NodeList转换为数组 getAttribute()
to get the for
attribute. getAttribute()
获得for
属性。 push
each for
into a new array. push
各for
进入一个新的阵列。 console.log(newArray)
Snippet 片段
var labelList = document.querySelectorAll('label'); var labelArray = Array.prototype.slice.call(labelList); var total = labelList.length; var forArray = []; for (var i = 0; i < total; i++) { var forAttr = labelArray[i].getAttribute('for'); forArray.push(forAttr); } console.log('for: ' + forArray);
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script> <div class="inline-group" id="album_set-group"> <h2>Albums</h2> <div class="inline-related"> <h3><b>Album:</b> <span class="inline_label">#1</span></h3> <fieldset class="module aligned "> <div class="form-row field-name"> <div> <label for="id_album_set-0-name">Name:</label> <input id="id_album_set-0-name" maxlength="100" name="album_set-0-name" type="text"> </div> </div> <div class="form-row field-release_date"> <div class="date-field"> <label for="id_album_set-0-release_date">Release date:</label> <input id="id_album_set-0-release_date" name="album_set-0-release_date" type="text"> </div> </div> </fieldset> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.