简体   繁体   English

jQuery将单击事件添加到未知的div ID

[英]JQuery add click event to unknown div id

I'm loading a php page that shows multiple elements, depending of the users (so i can't know how many items will be show). 我正在加载一个显示多个元素的php页面,具体取决于用户(因此我不知道将显示多少个项目)。 Those elements looks like : 这些元素看起来像:

<div id='container_feed'>
    <div id='feed_element' data-userid='4'>Random Dude</div>
    <div id='feed_element' data-userid='5'>Random Dude 2</div>
</div>

I'd like to add a click() event on each div and load a page depending of their userid HTML5 tag with .load() 我想在每个div上添加click()事件,并根据其带有.load()的用户ID HTML5标签加载页面

I tried child(), each(), and some other method whithout success... 我尝试了child(),each()和其他一些没有成功的方法...

How can i add this event ? 如何添加此事件?

Thanks. 谢谢。

Why not just delegate all the clicks like so: 为什么不像这样委派所有点击:

/* You could also use '>div' if you can't guarantee the class name */
$('#container_feed').on('click', '.feed-element' , function() {});

That way, you're only adding the one event initially, but all divs inside will be handled by the click event. 这样,您最初只添加了一个事件,但是内部的所有div都将由click事件处理。 No need to loop through each element, using .children or .each or even $('#container_feed .feed-element') . 无需遍历每个元素,使用.children.each甚至$('#container_feed .feed-element')

Delegation is your friend. 代表团是你的朋友。

First, don't use multiple IDs of same name. 首先,请勿使用同名的多个ID。 Use a CSS- class . 使用CSS class
Then access the userid in your click function like that: 然后像这样访问您的click函数中的用户ID:

Code Example: 代码示例:

$("div.feed_element").click(function(e) {
    var clickedUserId = $(this).data('userid');

    $('.userdata-container').load('ajax/userpage_' + clickedUserId +'.html');
});

Corresponding HTML: 对应的HTML:

<div id='container_feed'>
    <div class='feed_element' data-userid='4'>Random Dude</div>
    <div class='feed_element' data-userid='5'>Random Dude 2</div>
</div>
<div class='userdata-container'>
    <!-- The Ajax Code is loaded into this container -->
</div>

you can't have the same id for different elements, that's the point for an id (it should be unique). 对于不同的元素,您不能使用相同的ID,这就是ID的意义(它应该是唯一的)。 Use a class instead or just all div children from the main div 请改用一个班级,或者只使用主要div中的所有div子级

$('#container_feed div').on('click',function(){
  // do something, the the userid data, load the page somewhere, etc...
})

or use a class 或使用一堂课

html: 的HTML:

<div id='container_feed'>
  <div class='feed_element' data-userid='4'>Random Dude</div>
  <div class='feed_element' data-userid='5'>Random Dude 2</div>
</div>

js: js:

$('.feed_element').on('click',function(){
  // do something, the the userid data, load the page somewhere, etc...
})

newer versions of jquery recomends "on('click',function(){})" instead of "click(function(){})", i guess "click" will be deprecated sometime in the future and "on" can also replace "live()" 较新版本的jquery推荐使用“ on('click',function(){})”而不是“ click(function(){})”,我猜想“ click”将在未来某个时候被弃用,“ on”也可以替换“ live()”

Try like this.. 像这样尝试

$("div[id='feed_element']").click(function() {
     // Your stuff
});

You should use class, As ID is always used for unique identification. 您应该使用类,因为ID始终用于唯一标识。

First i would not use the same feed_element id for all divs. 首先,我不会对所有div使用相同的feed_element ID。

Try this 尝试这个

$('#container_feed').children().each(function(){
  if($(this).attr('data-userid') == # you want){
    $(this).click(function(){
      //your stuff here
    });
  }
});
$(".feed_element").click(function(event) {
    var userId = $(this).attr('data-userid');
    //make your ajax call.
});

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

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