如何使用jquery在区间内获取选定的id

时间:2017-07-06 06:05:38

标签: jquery

拥有这段代码并且我想获得所选元素id(popup_w)但是this.id或$(this).attr(' id')或$ this返回undefined ......

间隔有问题吗?我将如何获得所选元素ID?



console.clear();
$('#btn').on('click', function(){
   $("body").prepend('<div id="popup_w">A new popup div added</div>');
});

var found = false;
var checkDiv = setInterval( function() {
  
  if ( $("div[id$='_w']").length && !found) {  
    found = true;
    console.log( this.id ); 
  };
  
}, 1000);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="click" id="btn">
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:2)

您应该使用MutationObserver代替setInterval来更改DOM

$('#btn').on('click', function(){
   $(".container").prepend('<div id="popup_w">A new popup div added</div>');
});

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

var element = document.querySelector('.container');
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type == "childList") {
      console.log("New Node inserted with ID", mutation.addedNodes[0].id);
    }
  });
});

observer.observe(element, {
  childList: true //configure it to listen to attribute changes
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="click" id="btn">
<div class="container"></div>

答案 1 :(得分:1)

使用此功能获取Id d = a1 - a2 condlist = [d > min1, d < max1] choicelist = [3 * d, 5 * d] output = np.select(condlist, choicelist)

编辑:您可以使用$("div[id$='_w']")[0].id。感谢satpal提出建议。

$("div[id$='_w']").attr('id')
console.clear();
$('#btn').on('click', function(){
   $("body").prepend('<div id="popup_w">A new popup div added</div>');
});

var found = false;
var checkDiv = setInterval( function() {
  
  if ( $("div[id$='_w']").length && !found) {  
    found = true;
    console.log( $("div[id$='_w']")[0].id ); 
    console.log( $("div[id$='_w']").attr('id'));
  };
  
}, 1000);

答案 2 :(得分:1)

你可以做这样的事情

&#13;
&#13;
$(document).ready(function(){

console.clear();
$('#btn').on('click', function(){
    $("body").prepend('<div id="popup_w">A new popup div added</div>');
    $("body").prepend('<div id="popup2_w">A new popup div added</div>');
});

var found = false;
var checkDiv = setInterval( function() {
  if ( $("div[id$='_w']").length && !found) {  
    found = true;
     $("div[id$='_w']").each(function(){
        console.log( $(this).attr('id') ); 
     })
    
  };
  
}, 1000);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="click" id="btn">
&#13;
&#13;
&#13;

请注意,each循环非常有用,如果您希望获得具有_w属性id属性的几个div的ID。

答案 3 :(得分:1)

而不是使用setInterval使用jQuery自定义事件处理程序。

首先我们生成随机新ID,然后我们trigger新事件称为elementAdded

param1是您的id属性,您可以通过$('#' + param1);轻松访问新创建的元素

参见工作示例。

&#13;
&#13;
console.clear();
$('#btn').on('click', function(){
    var newId = makeid();
   $("body").prepend('<div id="' + newId + '">A new popup div added</div>');
   $("body").trigger("elementAdded", [ newId ]);
});

 $("body").on("elementAdded", function (event, param1) {
   // our new button
  var element = $('#' + param1);
  // Access your element $('#' + param1)
  console.log(element.length); // if 1 element is found any usable 
});
            
            

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="click" id="btn">
&#13;
&#13;
&#13;

答案 4 :(得分:0)

试试这个

 if ( $("div[id^='popup_']").length && !found) {  
    found = true;
    console.log($('div').attr('id')); 
  };

实施例: -

console.clear();
$('#btn').on('click', function(){
   $("body").prepend('<div id="popup_w">A new popup div added</div>');
});

var found = false;
var checkDiv = setInterval( function() {
  
  if ( $("div[id^='popup_']").length && !found) {  
    found = true;
    console.log($('div').attr('id')); 
  };
  
}, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="click" id="btn">

答案 5 :(得分:0)

您可以使用 .each 功能。

$('#btn').on('click', function(){
   $("body").prepend('<div id="popup_w">A new popup div added</div><div id="Spopup_w">A new popup div added</div>');
});

var found = false;
var checkDiv = setInterval( function() {

  if ( $("div[id$='_w']").length && !found) {  
    found = true;
    $("div[id$='_w']").each(function(){
        console.log(  this.id  ); 
    })

  };

}, 1000);