Javascript 在jquery上执行此操作的最短方法是什么?
这就是我所拥有的,它工作得很好,但是你能告诉我是否有任何简洁明了的编码方法来做到这一点吗Javascript 在jquery上执行此操作的最短方法是什么?,javascript,jquery,Javascript,Jquery,这就是我所拥有的,它工作得很好,但是你能告诉我是否有任何简洁明了的编码方法来做到这一点吗 $(document).ready(function() { $('span#paylas_').click(function() { $('#uyri').slideUp(); $('#dhda').slideUp(); $('#pyls').slideToggle('normal'); }); $('span#uyari_').
$(document).ready(function() {
$('span#paylas_').click(function() {
$('#uyri').slideUp();
$('#dhda').slideUp();
$('#pyls').slideToggle('normal');
});
$('span#uyari_').click(function() {
$('#pyls').slideUp();
$('#dhda').slideUp();
$('#uyri').slideToggle('normal');
});
$('span#dahada_').click(function() {
$('#pyls').slideUp();
$('#uyri').slideUp();
$('#dhda').slideToggle('normal');
});
});
如果为元素赋予
数据切换属性,如下所示:
<span id="paylas_" data-toggle="#pyls"></span>
如果仅对这些元素使用数据切换
,则还可以执行以下操作:
$('[data-toggle]').click(function() {
我真的不能对ID进行任何修改,但如果有某种结构,您也可以概括第二个选择器列表。这是一个快速重构,可能会有所帮助
$(document).ready(function() {
clickFuntion("paylas","uyri","dhda","pyls");
clickFuntion("uyari","pyls","dhda","uyri");
clickFuntion("dahada","pyls","uyri","dhda");
function clickFuntion(var1,va2,var3,var4){
$('span#"+var1+"_').click(function() {
$('#"+var2+"').slideUp();
$('#"+var3+"').slideUp();
$('#"+var4+"').slideToggle('normal');
});
}
});
下面是一个演示:我认为这是不链接HTML的最干净的方法:
$(document).ready(function() {
$("#paylas_, #uyari_, #dahada_").click(function() {
var ids = ["#uyri", "#dhda", "#pyls"],
current = "#" + $(this)
.attr("id")
.replace(/_$/, "")
.replace(/a(.)/g, "$1");
delete ids[$.inArray(current, ids)];
$(ids.join(",")).slideUp();
$(current).slideToggle("normal");
});
});
编辑:删除前面的span
它在很大程度上取决于您的html。所以请发布。@RonnieChesterLynwood:its数据切换
而不是数据切换
数据切换可以/应该缓存在文档中。就绪
,这样就不必每次点击都计算。@Jasper:这一点很好,尽管我不确定它是否对性能有那么大的影响。我编辑了我的答案。$('#paylas_uu,#uyari_u,#dahada_u')。每个(函数(I,o){toggles[this.id]=$(this.attr('data-toggle'))。点击(…)代码>--这将在文档中存储所有必要的信息。就绪
,因此在单击
事件处理程序中不必进行额外的处理。@Jasper:老实说,这种优化只适用于鼠标移动
,因为它执行了很多次。在单击处理程序中,这似乎有些过分。我给了你+1,但我认为最好只将id名放在转换结构中。如果此页面中存在任何动态行为,则存储jQuery对象(使用DOM元素)可能会导致内存泄漏。另外,这一行$('#uyri,#dhda,#pyls').not(convert[this.id]).slideUp()代码>不需要查找。它可以是这样:$('uyri,'dhda,'pyls')。而不是(this.slideUp()代码>@jfriend00谢谢你的建议。这对于选择.not()
很有意义,它似乎是最后一行convert[this.id]
,可能比在单击
事件处理程序中创建jQuery对象更快。这看起来正确吗?当然,预先创建jQuery对象会更快,但是在DOM中查找一个id的速度与不需要时不存储DOM元素的清洁度相比,在这里是无关紧要的。它只是开始一个动画,随着时间的推移,它会被一个用户事件启动——不可能被注意到。我也希望有更快的页面启动时间。为什么在选择器中将span放在ID前面?它只是让选择器引擎做了更多的工作。好问题!我不知道,我想我已经忘记了。@jfriend00我相信如果您想支持Internet Explorer 5.5(lol,我知道),那么您需要在选择器中使用标记名:
$(function() {//notice the shorter document.ready declaration :)
//setup a conversion from the clicked elements to the elements to slide up/down
var convert = {
paylas_ : '#pyls',
uyari_ : '#uyri',
dahada_ : '#dhda'
};
//select all the spans and bind the event handler to them, you can select multiple elements at once by separating the selectors with commas
$('#paylas_, #uyari_, #dahada_').click(function() {
//slide up all the spans except for the on associated with the currently clicked element
$('#uyri, #dhda, #pyls').not(this).slideUp();
//slide toggle the currently clicked element
$(convert[this.id]).slideToggle('normal');
});
});
$(document).ready(function() {
$("#paylas_, #uyari_, #dahada_").click(function() {
var ids = ["#uyri", "#dhda", "#pyls"],
current = "#" + $(this)
.attr("id")
.replace(/_$/, "")
.replace(/a(.)/g, "$1");
delete ids[$.inArray(current, ids)];
$(ids.join(",")).slideUp();
$(current).slideToggle("normal");
});
});