简体   繁体   English

如何获得已单击的Div的名称?

[英]How do I get the name of a Div that has been clicked?

I am using the following html 我正在使用以下html

<div id="chatid" name="1">
  <img src="http://placehold.it/50/55C1E7/fff" alt="User Avatar" class="img-circle" /> John
</div>
<div id="chatid" name="2">
  <img src="http://placehold.it/50/55C1E7/fff" alt="User Avatar" class="img-circle" /> James
</div> 

I am using the following jquery script 我正在使用以下jQuery脚本

$(document).ready(function() {
  $("#chatid").click(function() {
    thread = $(this).attr('name');
    $("#chatmessages").load('fetchmessages.php?id=' + thread);
    //alert($(this).attr('name'));
    return false;
  });
});

This only gives the name when clicking on the first "chatid" Div, is there a way for me to return the name of any "chatid" Div? 这仅在单击第一个“ chatid” Div时给出名称,我是否可以返回任何“ chatid” Div的名称?

Thanks 谢谢

$('div').click(function(){
    alert($(this).attr('name'));
});

Additionally, do not use the same ID for multiple elements as this is bad practice. 此外,请勿对多个元素使用相同的ID,因为这是不好的做法。 Use classes instead. 改用类。

See Why is it a bad thing to have multiple HTML elements with the same id attribute? 请参阅为什么具有多个具有相同id属性的HTML元素是一件坏事?

You have invalid html. 您的HTML无效。 There can be only one element with given id. 具有给定ID的元素只能是一个。

Use class instead: 使用类代替:

<div class="chatid" name="1">

then your selector will be: 那么您的选择器将是:

$(".chatid").click( //do something );

id is unique don't use repeatedly id是唯一的,请勿重复使用

<div class="chatid" name="1">
  <img src="http://placehold.it/50/55C1E7/fff" alt="User Avatar" class="img-circle" /> John
</div>
<div class="chatid" name="2">
  <img src="http://placehold.it/50/55C1E7/fff" alt="User Avatar" class="img-circle" /> James
</div>

JavaScript is JavaScript是

    $(document).ready(function() {
  $(".chatid").click(function() {
    var thread = $(this).attr('name');
    //$("#chatmessages").load('fetchmessages.php?id=' + thread);
    alert($(this).attr('name'));
    return false;
  });
});

Working example is Here 工作示例在这里

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

相关问题 如何添加一个计数器来显示一个 div 元素被点击了多少次? - How do I add a counter to show how many times a div element has been clicked? 如何根据在 React 中单击哪个按钮来切换 div class? - How do I toggle div class depending on which button has been clicked in React? 获取Phaser中被点击的精灵名称 - Get the name of the sprite that has been clicked on in Phaser 如何获得一个按钮被单击的次数的变量,将其在线存储然后在以后访问? - How do I get a variable of the number of times a button has been clicked, store it online and then access it later? 单击div时如何切换 - How to toggle when a div has been clicked 我如何获得一个按钮来执行多项任务,具体取决于单击了多少次? (JavaScript) - How can i get one button to do multiple tasks depending on how many times it has been clicked? (Javascript) 单击另一个DIV时如何使DIV消失? - How do I get DIV to disappear when another DIV is clicked? 如何检测已单击指向 javascript function 的链接的 div - How to detect the div in which a link to a javascript function has been clicked 如何确定单击哪个按钮的div /类 - How to determine which div/class a button has been clicked in 如何确定第一次加载时是否在jQuery中单击了某些内容? - How do I determine if something has been clicked in jQuery on the first load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM