开发者

How to combine these jquery functions?

is there a way to combine these ?

$("#demo1Btn").mopTip({'w':200,'style':"overClick",'get':"#demo1"});
$("#demo2Btn").mopTip({'w':200,'style':"overClick",'get':"#demo2"});
$("#demo3Btn").mopTip({'w':200,'style':"overClick",'get':"#demo3"});
$("#demo4Btn").mopTip({'w':200,'style':"overClick",'get':"#demo4"});
$("#d开发者_如何学Cemo5Btn").mopTip({'w':200,'style':"overClick",'get':"#demo5"});

Thnx


for (var i = 1; i < 6; i++) {
  $("#demo" + i + "Btn").mopTip({'w':200,'style':"overClick",'get':"#demo" + i});
}

would be a bit cleaner


Yes, you could place all the class/id's on a same function, but just separate with comma.

$("#demo1Btn, #demo2Btn, #demo3Btn, #demo4Btn, #demo5Btn").mopTip({'w':200,'style':"overClick",'get':$(this).attr('id').replace('Btn','')});

This should work!


Possibly with:

$('div[id^="demo"][id$="Btn"]').mopTip({'w':200,'style':"overClick",'get':this.id});

will select the elements whose id begins with "demo" and ends with "btn"; the this.id should find the relevant element's id and assign that to the get parameter.


Use the starts-with selector and an each loop

$("[id^='demo']").each(function(){
    $(this).mopTip({'w':200,'style':"overClick",'get':"#demo1"});
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜