引用类中的按钮ID

时间:2017-04-21 09:35:53

标签: jquery

<div id="dialogButtons" class="opportunityEditorPopup">     
    <span id="closed" class="label checkOption" style="display: none;"><input type="checkbox">Mark this as closed</span>
    <span class="testButtons" style="display: inline;">
            <button id="closeOpportunityButton" class="jqButton imaged ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" title="Close this opportunity" role="button"><span class="ui-button-text">Close</span></button>
            <button id="newOrderButton" class="jqButton imaged ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" title="Raise New Quote" role="button"><span class="ui-button-text">New Quote</span></button>
    </span>
</div>

所以我目前有以下HTML,我试图在JQuery中引用“closeOpportunityButton”。

然而,我遇到了这个问题,并尝试了以下方法:

$('.testButtons #closeOpportunityButton').click(function(){
    console.log("test")
});

$('#dialogButtons .testButtons #closeOpportunityButton').click(function(){
    console.log("test")
});

两者都不起作用。

想法?

1 个答案:

答案 0 :(得分:0)

尝试在您的选择中使用子选择器。以下是代码段:

&#13;
&#13;
$('.testButtons > #closeOpportunityButton').click(function(){
    console.log("test")
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="testButtons" style="display: inline;">
            <button id="closeOpportunityButton" class="jqButton imaged ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" title="Close this opportunity" role="button"><span class="ui-button-text">Close</span></button>
            <button id="newOrderButton" class="jqButton imaged ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" title="Raise New Quote" role="button"><span class="ui-button-text">New Quote</span></button>
    </span>
&#13;
&#13;
&#13;