简体   繁体   English

jQuery:获取html元素的ID:为什么一种解决方案有效而另一种不起作用

[英]Jquery: Getting the id of an html element: why one solution works and the other don't

I am referencing one element on an index page hidden initially and then showed with jquery on an index.html page. 我指的是最初隐藏的索引页面上的一个元素,然后在index.html页面上用jquery显示。 Can't explain why the first ones doesn't work, even if it should, but the last one does. 无法解释为什么第一个无效,即使可以,但是最后一个无效。 I am getting the id of a pressed button "id='edit1'...2...3..etc" Thank you! 我收到按下按钮的ID“ id ='edit1'... 2 ... 3..etc”,谢谢!

//why this doesn't work and the next one does???
/*$('.edit-btn').click(function(){
 var id = $(this).attr('id');
 console.log("btn1:", id);
 });

$('.edit-btn').on('click', function(){
 var id = $(this).attr('id');
 console.log("btn1:", id);
 });
 */

var id;
$("body").on("click",".edit-btn",function(){
id = (this.id).replace("edit","");

UPDATE: 更新:

The containers are hidden and shown using this function: 使用以下功能隐藏并显示容器:

function hideWindowsAndShowOneWindow(sWindowId) {
$('.wdw').hide(); //fadeout 500
$('#' + sWindowId).show(); // fade in 500
}

So the div container, I am trying to reference by id was hidden after the page was loaded or maybe before the page is loaded. 所以div容器,我试图通过id引用被隐藏在页面加载之后或页面加载之前。 My guess is after the page is loaded... 我的猜测是页面加载后...

The HTML: HTML:

<div class="wdw" id="wdw-events">

<h4>Events text</h4></br>
   <div id="content"></div>
    <h4>Edit Events</h4></br>
    <div>

</div>

It is added/injected after the page was loaded with this js script, but still what has the last method special, just referencing through the document down to the id attribute of the button: 在使用此js脚本加载页面后添加/注入了该脚本,但最后一个方法仍然很特殊,只是通过文档向下引用按钮的id属性:

  finalEventsLS.forEach(function (item) {
    var date = item.date.day + "/" +item.date.month + "/" + item.date.year;
    $('#content').append("<ul><li>" +
      "Event nr.: " + item.id + "   " +
      "Name: " + item.name + "   " +
      "Topic: "+ item.topic + "   " +
      "Speaker: "+ item.speaker + "   " +
      "Date: " + date + "&emsp;" +
      "</li><button class='edit-btn' id='edit" + item.id + "'>Edit</button>
   </ul>");

  })

It's hard to tell, since you've only shared your jQuery. 很难说,因为您只共享了jQuery。 Update with your HTML please. 请使用您的HTML更新。

If you are referencing an element which was added to the DOM after the initial page load, .click will not work, because it only looks for elements initially loaded into the DOM. 如果您引用的是在初始页面加载后添加到DOM中的元素,则.click将不起作用,因为它仅查找最初加载到DOM中的元素。 Instead, you would use the .on method, which looks for elements added to the DOM both before and after initial load. 而是使用.on方法,该方法在初始加载之前和之后查找添加到DOM中的元素。


UPDATE: 更新:

I should have looked closer. 我应该仔细看看。 The second snippet of code that didn't work, but used the .on method, most likely didn't work because you are still trying to access the .edit-btn element, which at this point I'm assuming was added after page load. 第二段代码无效,但是使用了.on方法,最有可能是无效的,因为您仍在尝试访问.edit-btn元素,这时我假设它是在页面之后添加的。加载。 The code snippet that does work, is accessing the body element first. 起作用的代码段首先访问body元素。 I will try to find and update with a better explanation, but the .on method still needs to find a element that existed in the DOM initially. 我将尝试使用更好的解释进行查找和更新,但是.on方法仍然需要查找最初存在于DOM中的元素。 From there, it can climb down the tree to find .edit-btn element. 从那里,它可以爬下树以找到.edit-btn元素。

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

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