0

html:

<a id="invitation" class="trigger" href="#"><img src="img.jpg"/></a>
<a id="dummy" class="hide">Do something</a>
<div id="invitationbox"></div>

当我这样做时,我得到了 jquery 代码:

$(".trigger").click(function() {
$('#invitation').load('invitation.php', function() {
$('#dummy').trigger('click');
});
});

但是希望它在类触发器的多个链接上工作......那么我如何重写代码以在多个地方工作?

示例:(无法使其正常工作...)

html:

<a id="anotherid" class="trigger" href="#"><img src="img.jpg"/></a>
<a class="hide">Do something</a>
<div id="anotheridbox"></div>

jQuery:

$(".trigger").click(function() {
var currentId = $(this).attr('id');
var contentId = $currentId + "box";
$($contentId).load('invitation.php', function() {
$(this).next("a").trigger('click');
});
});

帮我简化代码谢谢!:)

4

1 回答 1

0

在您的代码上下文中存在一些错误:

$(".trigger").click(function() {
    var currentId = $(this).attr('id');

    // $currentId is never declared
    var contentId = $currentId + "box";

    // $contentId is never declared and the id selector should be begin with a #
    $($contentId).load('invitation.php', function() {
        // $(this) is the element related to contentId, so there is 
        // no next("a") to trigger a click on
        $(this).next("a").trigger('click');
    });
});

试试这个:

$(".trigger").click(function() {
    var $trigger = $(this);
    $("#" + $trigger.attr('id') + 'box').load('invitation.php', function() {
        $trigger.next("a").click();
    });
});
于 2011-09-13T00:59:36.703 回答