假设我有这个标记:
<ul id="wizard">
<li>Step 1</li>
<li>Step 2</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我有这个jQuery:
$("#wizard li").click(function () {
// alert index of li relative to ul parent
});
Run Code Online (Sandbox Code Playgroud)
li单击该项时,如何获取相对于其父项的子项索引li?
例如,当您单击"步骤1"时,将alert弹出一个"0".
我在使用多个jQuery绑定数千个元素和输入时遇到加载速度问题,有没有更有效的方法呢?
该站点具有通过ajax调用在产品列表之间切换的能力,页面无法刷新.有些列表有10个项目,大约100个,有些超过2000个.当我开始在列表之间翻转时出现速度问题; 每次加载2000+项目列表时,系统拖动大约10秒钟.
在重建列表之前,我将目标元素的html设置为'',并取消绑定下面的两个绑定.我确定它与我在回调中所做的所有父,子和子调用有关.任何帮助深表感谢.
循环2500次
<ul>
<li><input type="text" class="product-code" /></li>
<li>PROD-CODE</li>
...
<li>PRICE</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
结束循环
$('li.product-code').bind( 'click', function(event){
selector = '#p-'+ $(this).prev('li').children('input').attr('lm');
$(selector).val(
( $(selector).val() == '' ? 1 : ( parseFloat( $(selector).val() ) + 1 ) )
);
Remote.Cart.lastProduct = selector;
Remote.Cart.Products.Push(
Remote.Cart.customerKey,
{
code : $(this).prev('li').children('input').attr('code'),
title : $(this).next('li').html(),
quantity : $('#p-'+ $(this).prev('li').children('input').attr('lm') ).val(),
price : $(this).prev('li').children('input').attr('price'),
weight : $(this).prev('li').children('input').attr('weight'),
taxable : $(this).prev('li').children('input').attr('taxable'),
productId : $(this).prev('li').children('input').attr('productId'),
links : $(this).prev('li').children('input').attr('productLinks')
},
'#p-'+ $(this).prev('li').children('input').attr('lm'),
false,
( parseFloat($(selector).val()) - 1 ) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Web应用程序并使用相当多的JavaScript执行任务.我有一些与Jquery绑定的标签点击做一些工作,所以我有这样的代码:
HTML代码:
<a href="#" id="id1">some content here</a>
<a href="#" id="id2">some content here</a>
....
Run Code Online (Sandbox Code Playgroud)
Jquery代码:
$('#id1').click(function(){
//do something here..
return false;
});
$('#id2').click(function(){
//do something else here..
return false;
});
Run Code Online (Sandbox Code Playgroud)
使用这种方法,当脚本运行时,jquery必须查找选择器(id1,id2,ect).
但还有另一种方法可以避免查找选择器,具体如下:
<a href="requireJs.htm" onclick="f1();return false">some content here</a>
<a href="requireJs.htm" onclick="f2();return false">some content here</a>
Run Code Online (Sandbox Code Playgroud)
和js代码:
function f1(){
//do something here..
});
function f2(){
//do something else here..
});
Run Code Online (Sandbox Code Playgroud)
考虑到性能,哪种方法更好?感谢帮助.
所以我使用append方法动态渲染一个带有jquery的段落,我想向它添加一个click事件但由于某种原因,click事件不起作用,我知道解决方案可能很简单,但我是jquery的新手并且会很感激任何帮助...我知道函数内部的代码是有效的,因为我用静态按钮测试它,它只是不使用动态的...请提前感谢任何帮助,
这是我的代码
$(this).parent().parent().children("div").append("<p class='tryAgain'>Try Again</p>");
Run Code Online (Sandbox Code Playgroud)
点击功能代码,
$(".tryAgain").click(function() {......}
Run Code Online (Sandbox Code Playgroud) 首先让我说我看过这篇文章.
它表示(和api.jquery.com一样).live()并且.on()让你分配一个jquery事件,它甚至适用于未来的元素.但是我的以下jquery:
$("#playerImg").on("mouseenter",function () {
console.log("works");});
Run Code Online (Sandbox Code Playgroud)
在开始时工作,但在我运行此代码后:
function move(moveTo) {
document.getElementById(player.currentLocation).innerHTML = "";
player.currentLocation = moveTo;
document.getElementById(player.currentLocation).innerHTML += player.displayText;
}
Run Code Online (Sandbox Code Playgroud)
这是相关的变量
var player = {
"displayText" : "<img src = 'http://3.bp.blogspot.com/-kAhN0HX-MBk/T_5bApfhbJI/AAAAAAAAAuI/lUww8xT9yV8/s1600/smileys_001_01.png'" +
id='playerImg' "class='onTop' alt='you' border='0' />",
"currentLocation": 0 //(i use a 5 * 5 table with id's 0-24)
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有运行.我如何在jquery中创建一个事件,它可以在不存在的元素上运行?