简体   繁体   English

使用jquery获取ul中每个li的ID或名称

[英]Get Id or Name of each li in ul with jquery

I want to get id and text name of each li in ul using jquery 我想使用jQuery获取UL中每个李的ID和文本名称

I have tried this but it didn't work 我已经尝试过了,但是没有用

<ul id="tags1">
    @foreach (var item in Model.Tags)
    {             
        <li id=@item.TagName >@item.TagName</li>    
    }    
</ul>

I have tried this code but it gave me a lot or html 我已经尝试过此代码,但它给了我很多或HTML

$('#tags1 li').each(function (i) {
    alert($(this).html());
    console.log($(this).html());
});

It gave me that 它给了我

<span class="tagit-label">Art</span><a class="tagit-close"><span class="text-icon">×</span><span class="ui-icon ui-icon-close"></span></a><input type="hidden" style="display:none;" value="Art" name="tags">

I just need to get the value only value="Art" 我只需要获取value only value =“ Art”

Thank you 谢谢

You can do it like following. 您可以按照以下步骤进行操作。

$('#tags1 li').each(function () {
    console.log(this.id);  //id
    console.log($('.tagit-label', this).text());  //text
});
$('#tags1 li').each(function () {
    console.log(this.id);
    console.log($(this).text());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM