如何点击功能为一个自动建立的li列表
How to click function for an auto built li list
在我的应用程序中,它解析xml文件中的数据,然后将数据显示在<li>
列表中。因此,有一个<ul>
列表和一个xml文件循环,用于在每个<li>
标记中放置一些特定数据。它成功地读取了xml文件并创建了列表,但问题是列表有点不活动。例如,我制作了一个click
函数,因此当单击<li>
时,它会给出alert("done!");
,但它不起作用。
这是我的代码:
var tracksArray = [];
$.ajax({
url: 'https://dl.dropboxusercontent.com/u/33538012/playlist.xml',
dataType: "xml",
success: parse,
error: function() {
alert("Error: Something went wrong");
}
});
function parse(document) {
$(document).find("track").each(function() {
tracksArray.push($(this).find('url').text());
$(".panel1 ul").append(
"<li id='row" + tracksArray.length + "'>" +
"<p class='title'>" + $(this).find('title').text() + "</p>" +
"</li>"
);
});
}
$(".panel1 ul li").on("click", function() {
alert("done!");
})
div.app {
margin: 50px auto;
width: 400px;
height: 400px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
div.app > .blur {
width: 100%;
height: 100%;
-webkit-filter: blur(5px);
}
div.mainSection,
div.dashboard {
position: absolute;
left: 0px;
text-align: center;
color: #fff;
font-size: 20px;
}
div.mainSection {
width: 100%;
height: 85%;
background: rgba(0, 0, 0, 0.5);
top: 0;
}
div.dashboard {
width: 100%;
height: 15%;
background: rgba(255, 0, 0, 0.5);
bottom: 0;
}
div.mainSection > .panel1,
div.mainSection > .panel2,
div.mainSection > .panel3 {
width: 100%;
Height: 100%;
Background: rgba(0, 0, 0, 0.5);
position: absolute;
left: 0px;
top: 0px;
}
div.mainSection > .panel3 > p {
margin-top: 80px;
}
.grid-button {
background: none;
border: none;
padding: 3px;
width: 100%;
}
.grid {
display: inline-block;
height: 4px;
position: relative;
width: 32px;
transition: all 0.3s ease-in-out;
}
.grid:after,
.grid:before {
content: '';
position: absolute;
background-color: #FFF;
display: inline-block;
height: 4px;
left: 0;
width: 32px;
transition: all 0.3s ease-in-out;
}
.grid.open {
background-color: #FFF;
}
.grid.open:after {
top: 10px;
}
.grid.open:before {
top: -10px;
}
.grid.close {
background-color: transparent;
transform: scale(0.9);
}
.grid.close:after,
.grid.close:before {
top: 0;
transform-origin: 50% 50%;
}
.grid.close:before {
transform: rotate(135deg);
}
.grid.close:after {
transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="app">
<div class="blur"></div>
<div class="mainSection">
<div class="panel1">
<ul></ul>
</div>
<div class="panel2" style="display: none;"></div>
<div class="panel3" style="display: none;"></div>
</div>
<div class="dashboard"></div>
</div>
正如您所看到的,我的js代码的最后一部分是用于在单击<li>
时发出警报,但什么也并没有发生。
有人知道为什么吗?
因为您将onclick
侦听器直接添加到li
元素,所以如果通过AJAX回调将li
从ul
中常规添加/删除,它将不起作用。当您第一次将onclick
附加到尚未附加到ul
的li
时,它将不起任何作用。
这可以通过将onclick
侦听器附加到ul
本身来实现。然后检查目标CCD_ 17是否在CCD_。
$('panel1 ul').on('click', 'li', function() {
console.log('list clicked!');
});
这意味着panel1 ul
中的每一个被点击的元素都将被委派到ul
。如果是li
,则将执行回调。
您可以使用委托:
$(document).on("click", "pane1 ul li", function(){ ... });
样品
function createLI() {
var html = "";
for (var i = 0; i < 5;) {
html += "<li>" + ++i + "</li>";
}
$("#list").append(html);
}
function registerEvents() {
$(document).on("click", "#content ul li", function() {
alert($(this).text());
})
}
registerEvents();
createLI();
li {
background: #eee;
margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="content">
<ul id="list"></ul>
</div>
这是因为javascript运行asyn
当数据仍在等待ajax时。
$(".panel1 ul li").on("click", function() {
alert("done!");
})
已执行
这就是为什么它不起作用
您可以简单地将on()函数放在parse()函数的末尾
这是我的jsbin
https://jsbin.com/jicawe/edit?html,css,js,输出
稍作更改,将单击事件移动到function parse
中。由于ajax是异步的,当执行$.ajax
时,它在另一个线程中执行,同时执行$(".panel1 ul li").on("click",...)
,但发现选择器一无所获。因此,将点击事件移动到解析函数中可以确保在ajax完成后添加事件。
function parse(document) {
$(document).find("track").each(function() {
tracksArray.push($(this).find('url').text());
$(".panel1 ul").append(
"<li id='row" + tracksArray.length + "'>" +
"<p class='title'>" + $(this).find('title').text() + "</p>" +
"</li>"
);
});
$(".panel1 ul li").on("click", function() {
alert("done!");
});
}
您可以按照以下方式进行操作。
此处应用事件传播的概念
因此,您可以获得点击事件,如:
$('.panel1').on("click", "ul li", function(){
var tracksArray = [];
$.ajax({
url: 'https://dl.dropboxusercontent.com/u/33538012/playlist.xml',
dataType: "xml",
success: parse,
error: function() {
alert("Error: Something went wrong");
}
});
function parse(document) {
$(document).find("track").each(function() {
tracksArray.push($(this).find('url').text());
$(".panel1 ul").append(
"<li id='row" + tracksArray.length + "'>" +
"<p class='title'>" + $(this).find('title').text() + "</p>" +
"</li>"
);
});
}
$('.panel1').on("click", "ul li", function(){
alert( $( this ).children( "p" ).text());
})
div.app {
margin: 50px auto;
width: 400px;
height: 400px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
div.app > .blur {
width: 100%;
height: 100%;
-webkit-filter: blur(5px);
}
div.mainSection,
div.dashboard {
position: absolute;
left: 0px;
text-align: center;
color: #fff;
font-size: 20px;
}
div.mainSection {
width: 100%;
height: 85%;
background: rgba(0, 0, 0, 0.5);
top: 0;
}
div.dashboard {
width: 100%;
height: 15%;
background: rgba(255, 0, 0, 0.5);
bottom: 0;
}
div.mainSection > .panel1,
div.mainSection > .panel2,
div.mainSection > .panel3 {
width: 100%;
Height: 100%;
Background: rgba(0, 0, 0, 0.5);
position: absolute;
left: 0px;
top: 0px;
}
div.mainSection > .panel3 > p {
margin-top: 80px;
}
.grid-button {
background: none;
border: none;
padding: 3px;
width: 100%;
}
.grid {
display: inline-block;
height: 4px;
position: relative;
width: 32px;
transition: all 0.3s ease-in-out;
}
.grid:after,
.grid:before {
content: '';
position: absolute;
background-color: #FFF;
display: inline-block;
height: 4px;
left: 0;
width: 32px;
transition: all 0.3s ease-in-out;
}
.grid.open {
background-color: #FFF;
}
.grid.open:after {
top: 10px;
}
.grid.open:before {
top: -10px;
}
.grid.close {
background-color: transparent;
transform: scale(0.9);
}
.grid.close:after,
.grid.close:before {
top: 0;
transform-origin: 50% 50%;
}
.grid.close:before {
transform: rotate(135deg);
}
.grid.close:after {
transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="app">
<div class="blur"></div>
<div class="mainSection">
<div class="panel1">
<ul></ul>
</div>
<div class="panel2" style="display: none;"></div>
<div class="panel3" style="display: none;"></div>
</div>
<div class="dashboard"></div>
</div>
希望能有所帮助。
给你的li
类;
$(".panel1 ul").append(
"<li class="lis" id='row" + tracksArray.length + "'>" +
"<p class='title'>" + $(this).find('title').text() + "</p>" +
"</li>"
);
然后尝试;
$(".lis").live("click", function(){
alert("done!");
});
- 要求输入在数据列表中
- 从javascript创建一个列表
- 如何使用jQuery选择下拉列表的值
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- Javascript按钮下拉列表
- 如何使用json将对象列表从java转换为javascript
- 区分JSON中的矩阵和列表列表
- jQuery表单添加不适用于下拉列表
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 接受不在列表中的值-引导组合框
- 禁用jQuery中的下拉列表
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 如何在javascript中迭代数字列表
- 如何在按钮中显示下拉列表中选定的元素
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 如何建立具有youtube视频的视频播放列表
- 如何点击功能为一个自动建立的li列表
- 如何使第一个单选按钮在默认情况下选择一个列表中建立的for in循环使用trick