jquery怎么获取li的值

在jQuery中,我们可以通过多种方式获取li的值,以下是一些常见的方法:

jquery怎么获取li的值
(图片来源网络,侵删)

1、使用.text()方法获取li的文本值

.text()方法是jQuery中最常用的方法之一,用于获取或设置元素的文本内容,要获取li元素的文本值,只需将该方法应用于li元素即可。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF8">
    <meta name="viewport" content="width=devicewidth, initialscale=1.0">
    <title>jQuery获取li的值</title>
    <script src="https://code.jquery.com/jquery3.6.0.min.js"></script>
</head>
<body>
    <ul>
        <li>苹果</li>
        <li>香蕉</li>
        <li>橙子</li>
    </ul>
    <button id="getValue">获取li的值</button>
    <script>
        $(document).ready(function(){
            $("#getValue").click(function(){
                $("li").each(function(){
                    console.log($(this).text());
                });
            });
        });
    </script>
</body>
</html>

在这个示例中,我们创建了一个包含三个li元素的无序列表,当用户点击“获取li的值”按钮时,我们将使用.text()方法遍历所有li元素并打印它们的文本值。

2、使用.val()方法获取li的值(适用于下拉列表)

.val()方法通常用于表单元素,如输入框和下拉列表,要获取下拉列表中选中的li的值,我们可以使用以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF8">
    <meta name="viewport" content="width=devicewidth, initialscale=1.0">
    <title>jQuery获取li的值(下拉列表)</title>
    <script src="https://code.jquery.com/jquery3.6.0.min.js"></script>
</head>
<body>
    <select id="fruits">
        <option value="apple">苹果</option>
        <option value="banana">香蕉</option>
        <option value="orange">橙子</option>
    </select>
    <button id="getValue">获取li的值</button>
    <script>
        $(document).ready(function(){
            $("#getValue").click(function(){
                var selectedValue = $("#fruits option:selected").val();
                console.log(selectedValue);
            });
        });
    </script>
</body>
</html>

在这个示例中,我们创建了一个包含三个选项的下拉列表,当用户点击“获取li的值”按钮时,我们将使用.val()方法获取选中的li的值并打印出来。

3、使用.attr()方法获取li的值(适用于自定义属性)

如果我们为li元素添加了自定义属性,可以使用.attr()方法获取这些属性的值。

<br/> <span style="fontfamily: Arial;"><span style="fontsize: small;"><strong>&lt;!DOCTYPE html&gt;</strong></span><br/><br/> &lt;html&nbsp;lang=&quot;en&quot;&gt;<br/> &lt;head&gt;<br/> &nbsp;&nbsp;&lt;meta&nbsp;charset=&quot;UTF8&quot;&gt;<br/> &nbsp;&nbsp;&lt;meta&nbsp;name=&quot;viewport&quot;&nbsp;content=&quot;width=devicewidth,&nbsp;initialscale=1.0&quot;&gt;<br/> &nbsp;&nbsp;&lt;title&gt;jQuery获取li的值(自定义属性)&lt;/title&gt;<br/> &nbsp;&nbsp;&lt;script&nbsp;src=&quot;https://code.jquery.com/jquery3.6.0.min.js&quot;&gt;&lt;/script&gt;<br/> &lt;/head&gt;<br/> &lt;body&gt;<br/> &nbsp;&nbsp;&lt;ul&gt;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&lt;li&nbsp;dataid=&quot;1&quot;&gt;苹果&lt;/li&gt;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&lt;li&nbsp;dataid=&quot;2&quot;&gt;香蕉&lt;/li&gt;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&lt;li&nbsp;dataid=&quot;3&quot;&gt;橙子&lt;/li&gt;<br/> &nbsp;&nbsp;&lt;/ul&gt;<br/> &nbsp;&nbsp;&lt;button&nbsp;id=&quot;getValue&quot;&gt;获取li的值&lt;/button&gt;<br/> &lt;script&gt;<br/> &nbsp;&nbsp;(document).ready(function(){<br/> &nbsp;&nbsp;&nbsp;&nbsp;(#getValue).click(function(){<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(li).each(function(){<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(console.log($(this).attr(&apos;dataid&apos;));)})})})})&lt;/script&gt;<br/>&lt;/body&gt;</span></span></div> <div style="fontfamily: Arial, PingFang, PingFangSC, &quot;Microsoft YaHei&quot;, 微软雅黑, 宋体, Tahoma, sansserif; fontsize: 14px; lineheight: 24px"><br></div>

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/372290.html

(0)
酷盾叔的头像酷盾叔订阅
上一篇 2024-03-22 21:59
下一篇 2024-03-22 22:00

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入