开发者

Find and attach container dynamically

开发者 https://www.devze.com 2023-01-08 07:53 出处:网络
HTML: a#myname div#tooltip-myname a#yourname div#tooltip-yourname jQuery: $(\'#myname\').tooltip($(\'#tooltip-myname\'));

HTML:

a#myname
div#tooltip-myname

a#yourname
div#tooltip-yourname

jQuery:

$('#myname').tooltip($('#tooltip-myname'));
$('#yourname').t开发者_如何转开发ooltip($('#tooltip-yourname'));

How do I automate the tooltip container part so I don't have to manually enter '#tooltip-myname' '#tooltip-yourname' and so on with each tooltip?

Thanks!


You can use a .each() loop, like this:

$('#myname, #yourname').each(function() {
 $(this).tooltip($('#tooltip-' + this.id));
});

If those elements had a class, it gets easier to maintain, for example if they both had class="hasTooltip" you could use .class selector instead, like this:

$('.hasTooltip').each(function() {
 $(this).tooltip($('#tooltip-' + this.id));
});

Then you could add as many as you wanted without editing the script.

0

精彩评论

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