Jquery 如何访问li元素的名称或值?
我有一个li元素,看起来像这样:Jquery 如何访问li元素的名称或值?,jquery,Jquery,我有一个li元素,看起来像这样: <ol id="selectbox"> <li class="boxes" name="bears">bees</li> <li class="boxes" name="cakes">candles</li> <li class="boxes" name="wine">beer</li> <li class="boxes" name="spo
<ol id="selectbox">
<li class="boxes" name="bears">bees</li>
<li class="boxes" name="cakes">candles</li>
<li class="boxes" name="wine">beer</li>
<li class="boxes" name="spoon">fork</li>
<li class="boxes" name="bench">chair</li>
<li class="boxes" name="matches">fire</li>
<li class="boxes" name="kindling">wood</li>
</ol>
及
及
等等。用于获取文本
$(this).text()
要知道名字
$(this).attr('name')
用于获取文本
$(this).text()
要知道名字
$(this).attr('name')
您可以在单击事件上下文中使用$(this)
,因为它对应于单击的元素
var name = $(this).attr('name'); // bears
var val = $(this).text(); // bees
代码
$('#selectbox li').click(function(){
var $this = $(this);
var $btn = $this.addClass('active');
var name = $this.attr('name');
var txt = $this.text();
console.log("Name Attribute - " + name + " :: Text - "+ txt);
});
另外,最好对自定义属性使用数据属性,这样文档将被验证并符合标准实践
所以
您可以在单击事件上下文中使用$(this)
,因为它对应于单击的元素
var name = $(this).attr('name'); // bears
var val = $(this).text(); // bees
代码
$('#selectbox li').click(function(){
var $this = $(this);
var $btn = $this.addClass('active');
var name = $this.attr('name');
var txt = $this.text();
console.log("Name Attribute - " + name + " :: Text - "+ txt);
});
另外,最好对自定义属性使用数据属性,这样文档将被验证并符合标准实践
所以
就这么简单:
$('#myOL li').click(function(){
$(this).addClass('active');
var name = $(this).attr('name');
var text = $(this).text();
});
就这么简单:
$('#myOL li').click(function(){
$(this).addClass('active');
var name = $(this).attr('name');
var text = $(this).text();
});
那么
返回#selectbox中包含类框的所有元素的数组
通过使用jQuery使您的生活更轻松,您可以做到这一点
$('#selectbox .boxes').click(function(){
var tmp_name = $(this).attr('name');
alert(tmp_name);
alert($(this).text()); // bees
alert($(this).html()); // bees
// changes the attribute name of this li element which has class boxes and has been clicked.
$(this).attr('name','Hello world!');
// changes the inner html of this li element which has class boxes and has been clicked.
$(this).html('Hello world!');
})
希望有帮助。嗯
返回#selectbox中包含类框的所有元素的数组
通过使用jQuery使您的生活更轻松,您可以做到这一点
$('#selectbox .boxes').click(function(){
var tmp_name = $(this).attr('name');
alert(tmp_name);
alert($(this).text()); // bees
alert($(this).html()); // bees
// changes the attribute name of this li element which has class boxes and has been clicked.
$(this).attr('name','Hello world!');
// changes the inner html of this li element which has class boxes and has been clicked.
$(this).html('Hello world!');
})
希望有帮助。太好了。非常感谢。我没有意识到我可以如此轻松地访问阵列。这是首选做法吗?我想知道如果这个类(box)没有出现在这样一个上下文之外,那么它是否比(“#myOL li”)更好…最好指定在哪里查找这些.box,这样您就不会扫描整个文档了$(“#selectbox.box”)获取id为selectbox的元素,并查找其中包含类框的元素。速度更快。只需确保只有一个id为selectbox.great的元素。非常感谢。我没有意识到我可以如此轻松地访问阵列。这是首选做法吗?我想知道如果这个类(box)没有出现在这样一个上下文之外,那么它是否比(“#myOL li”)更好…最好指定在哪里查找这些.box,这样您就不会扫描整个文档了$(“#selectbox.box”)获取id为selectbox的元素,并查找其中包含类框的元素。速度更快。只需确保只有一个id为selectbox的元素。
$('#selectbox .boxes').click(function(){
var tmp_name = $(this).attr('name');
alert(tmp_name);
alert($(this).text()); // bees
alert($(this).html()); // bees
// changes the attribute name of this li element which has class boxes and has been clicked.
$(this).attr('name','Hello world!');
// changes the inner html of this li element which has class boxes and has been clicked.
$(this).html('Hello world!');
})