联系我们 / Contact us

   邯郸市拓航金属制品制造有限公司
   电 话:17790068725
   手 机:13332133228
   E-mail :26144244@qq.com
   公司地址:北京市大兴区66号

首页>>新闻中心新闻中心/ News

jquery怎么获取li的值
来源:互联网    发布时间:2026-11-30 08:05:33

在jQuery中,我们可以通过多种方式(shi)获取li的值,以下是一些常见(jian)的方法:(图片来源网络,侵删)

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

jquery怎么获取li的值

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

jquery怎么获取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>

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

jquery怎么获取li的值

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

.val()方法(fa)通常用于表单元素,如输入框和下拉列表,要获取下拉列表中选中的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 selected='selected'Value = $("#fruits option:selected=""").val(); console.log(selected=""Value); }); }); </script></body></html>

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

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>

上一条: 黄冈网站推广软件是多少_黄冈搜索引擎推广哪家好
下一条: 龙岩网站设计_龙岩专业网站定制开发哪里比较好