如何为<li>标签使用其id添加动作监听器?

huangapple go评论51阅读模式
英文:

How to add an action listener for a <li> tag using its id?

问题

我正在创建一个li标签,一旦用户点击它,它将使用JavaScript在控制台上发布一些内容。

这是我的index.html:

<!-- 开始片段:不隐藏 js 控制台: true 不使用 Babel: false -->

<!-- 语言: lang-js -->

    $('#Title').on('click', function(){
      console.log("List was clicked.");  
    });

<!-- 语言: lang-html -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <li id="Title"> List 1 </li>
    <li id="Title"> List 2 </li>
    <li id="Title"> List 3 </li>

<!-- 结束片段 -->

但它没有任何作用,有什么解决方法吗?

英文:

I am creating a li tag that once the user clicks it, it will post something on the console using javascript.

Here is my index.html:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#Title&#39;).on(&#39;click&#39;, function(){
  console.log(&quot;List was clicked.&quot;);  
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;li id=&quot;Title&quot;&gt; List 1 &lt;/li&gt;
&lt;li id=&quot;Title&quot;&gt; List 2 &lt;/li&gt;
&lt;li id=&quot;Title&quot;&gt; List 3 &lt;/li&gt;

<!-- end snippet -->

But it doesn't do anything, are there any approach to this?

答案1

得分: 0

问题出在您在列表中的每个项目上使用相同的 'id' 属性。它必须在文档中是唯一的。如果您想要相同的名称,可以使用类。请参见下面的示例:

HTML:

&lt;li class=&quot;Title&quot;&gt;List 1&lt;/li&gt;
&lt;li class=&quot;Title&quot;&gt;List 2&lt;/li&gt;
&lt;li class=&quot;Title&quot;&gt;List 3&lt;/li&gt;

JS:

$(&#39;.Title&#39;).on(&#39;click&#39;, function(){
    console.log(&quot;List was clicked.&quot;);
});
英文:

The issue is because you are using the same id attribute for each item in the list. It has to be unique in the document. You can use a class if you want to give the same name. See Example below:

HTML:

&lt;li class=&quot;Title&quot;&gt;List 1&lt;/li&gt;
&lt;li class=&quot;Title&quot;&gt;List 2&lt;/li&gt;
&lt;li class=&quot;Title&quot;&gt;List 3&lt;/li&gt;

JS:

$(&#39;.Title&#39;).on(&#39;click&#39;, function(){
    console.log(&quot;List was clicked.&quot;);
});

答案2

得分: 0

你不能拥有具有相同ID的多个标签

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
$(&#39;#Title2&#39;).on(&#39;click&#39;, function(){
    console.log(&quot;List 2 was clicked.&quot;)
});

<!-- 语言: lang-html -->
&lt;li id=&quot;Title1&quot;&gt; List 1 &lt;/li&gt;
&lt;li id=&quot;Title2&quot;&gt; List 2 &lt;/li&gt;
&lt;li id=&quot;Title3&quot;&gt; List 3 &lt;/li&gt;

<!-- 结束代码片段 -->
英文:

You cannot have multiple tags that have a same ID

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#Title2&#39;).on(&#39;click&#39;, function(){
    console.log(&quot;List 2 was clicked.&quot;)  });

<!-- language: lang-html -->

&lt;li id=&quot;Title1&quot;&gt; List 1 &lt;/li&gt;
&lt;li id=&quot;Title2&quot;&gt; List 2 &lt;/li&gt;
&lt;li id=&quot;Title3&quot;&gt; List 3 &lt;/li&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 14:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定