无论我在div中点击哪里,我想获得我的数据集值

时间:2018-01-01 04:59:09

标签: javascript jquery html css

所以,伙计们请访问我的codeSnippet。我现在无法在3天内尝试这个问题找到答案。

我想要实现的目标

  1. 用户点击按钮。
  2. 显示3个带红色边框的框。
  3. 点击红色框中的任意位置应该会给我这个方框的水果名称
  4. 现在的问题是:当我点击某些区域时它会工作,但其他区域给我未定义。我想在js部分的最后一行中调用div小部件而不是allresults div,但没有任何反应根本没有警告:

    $('#widget').on('click',  testAlert);
    

    $("#test").on('click',  begin);
    
    function begin(){
      var test = ["apple","pear","banana"];
      var test2 = ["fish", "bird", "insect"];
      var test3 = ["iOS", "Android", "Windows"];
      var test4 = ["Mom", "Dad", "Sis"];
      
      for (var i=0; i < test.length; i++){
        
        var listItem = document.createElement('div'),
        html = "<p id='one'>" + test[i] + '</p>' 
             + "<p id='two'> " + test2[i] + '</p>'
             + "<p id='three'>" + test3[i] + '</p>'
             + "<p id='four'> " + test4[i] + '</p>';
         listItem.id='widget';
                      
          
        listItem.dataset.fruit = test[i];
        listItem.innerHTML = html;
        allresults.appendChild(listItem);
      }
    }
    
    function testAlert(e){
    		var fruit = e.target.dataset.fruit; 
        alert(fruit);
    }
    
    $('#allresults').on('click',  testAlert);
    #one{
      margin-left:12%;
      padding-top:8%;
      color:black;
      font-family:arial;
      font-size:15px;
    }
    
    #two{
      margin-left:3%;
      color:black;
      font-family:arial;
      font-size:17px;
      line-height:5px;
    }
    
    #three{
      margin-left:3%;
      color:black;
      font-family:arial;
      font-size:12px;
      line-height:10px;
    }
    
    #four{
      margin-left:3%;
      padding-bottom:8%;
      color:black;
      font-family:testFont1;
      font-size:12px;
      line-height:-10px;
    }
    
    #widget{
      border:1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <div id="searchResults">        
        <div id="allresults" >              
        </div>        
    </div>
    
    <button id=test>click me</button>

2 个答案:

答案 0 :(得分:2)

您需要attach an event handler to the dynamically-created DIV elements,而不是#allresults

&#13;
&#13;
$("#test").on('click',  function (){
  var test = ["apple","pear","banana"];
  var test2 = ["fish", "bird", "insect"];
  var test3 = ["iOS", "Android", "Windows"];
  var test4 = ["Mom", "Dad", "Sis"];
  
  for (var i=0; i < test.length; i++){
    
    var listItem = document.createElement('div'),
    html = "<p id='one'>" + test[i] + '</p>' 
         + "<p id='two'> " + test2[i] + '</p>'
         + "<p id='three'>" + test3[i] + '</p>'
         + "<p id='four'> " + test4[i] + '</p>';
     listItem.id='widget';
                  
      
    listItem.dataset.fruit = test[i];
    listItem.innerHTML = html;
    allresults.appendChild(listItem);
  }
});


// add an onclick handler to dynamically-created DIV
$('#allresults').on('click', 'div', function () {
		var fruit = this.dataset.fruit; 
    alert(fruit);
});
&#13;
#one{
  margin-left:12%;
  padding-top:8%;
  color:black;
  font-family:arial;
  font-size:15px;
}

#two{
  margin-left:3%;
  color:black;
  font-family:arial;
  font-size:17px;
  line-height:5px;
}

#three{
  margin-left:3%;
  color:black;
  font-family:arial;
  font-size:12px;
  line-height:10px;
}

#four{
  margin-left:3%;
  padding-bottom:8%;
  color:black;
  font-family:testFont1;
  font-size:12px;
  line-height:-10px;
}

#widget{
  border:1px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="searchResults">        
    <div id="allresults" >              
    </div>        
</div>

<button id=test>click me</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是因为点击时的e.target可以是widget div元素中的child(p)元素。在这种情况下,您必须访问其父元素,如下所示:

function testAlert(e){
  var t = e.target;
  if (t.id != "widget") t = t.parentElement;
  var fruit = t.dataset.fruit; 
  alert(fruit);
}

这是完整的代码段:

&#13;
&#13;
$("#test").on('click',  begin);

function begin(){
  var test = ["apple","pear","banana"];
  var test2 = ["fish", "bird", "insect"];
  var test3 = ["iOS", "Android", "Windows"];
  var test4 = ["Mom", "Dad", "Sis"];
  
  for (var i=0; i < test.length; i++){
    
    var listItem = document.createElement('div'),
    html = "<p id='one'>" + test[i] + '</p>' 
         + "<p id='two'> " + test2[i] + '</p>'
         + "<p id='three'>" + test3[i] + '</p>'
         + "<p id='four'> " + test4[i] + '</p>';
     listItem.id='widget';
      
    listItem.dataset.fruit = test[i];
    listItem.innerHTML = html;
    allresults.appendChild(listItem);
  }
}

function testAlert(e){
	var t = e.target;
	if (t.id != "widget") t = t.parentElement;
	var fruit = t.dataset.fruit; 
    alert(fruit);
}

$('#allresults').on('click',  testAlert);
&#13;
#one{
  margin-left:12%;
  padding-top:8%;
  color:black;
  font-family:arial;
  font-size:15px;
}

#two{
  margin-left:3%;
  color:black;
  font-family:arial;
  font-size:17px;
  line-height:5px;
}

#three{
  margin-left:3%;
  color:black;
  font-family:arial;
  font-size:12px;
  line-height:10px;
}

#four{
  margin-left:3%;
  padding-bottom:8%;
  color:black;
  font-family:testFont1;
  font-size:12px;
  line-height:-10px;
}

#widget{
  border:1px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="searchResults">        
    <div id="allresults" >              
    </div>        
</div>

<button id=test>click me</button>
&#13;
&#13;
&#13;