所以,伙计们请访问我的codeSnippet。我现在无法在3天内尝试这个问题找到答案。
我想要实现的目标
现在的问题是:当我点击某些区域时它会工作,但其他区域给我未定义。我想在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>
答案 0 :(得分:2)
您需要attach an event handler to the dynamically-created DIV elements,而不是#allresults
。
$("#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;
答案 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);
}
这是完整的代码段:
$("#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;