我有以下代码:
var buttonOne = $("#buttonOne");
var buttonTwo = $("#buttonTwo");
// opens an already initialized modal window
(buttonOne, buttonTwo).click(function()
{
modalBoxContainer.dialog("open");
});
Run Code Online (Sandbox Code Playgroud)
这不起作用.如果我使用元素,它将起作用,但不会与变量一起使用.
有任何想法吗?
尝试组合选择器?
$("#buttonOne, #buttonTwo").click(...
Run Code Online (Sandbox Code Playgroud)
尽管您确实应该使用on
(jQuery 1.7) 或delegate
1.7 之前的版本,但这将为处理程序中的所有元素创建一个事件。就像是:
$(document).on("click", "#buttonOne, #buttonTwo", function () {
//function here
});
Run Code Online (Sandbox Code Playgroud)
on
文档: http: //api.jquery.com/on/
delegate
文档: http: //api.jquery.com/delegate/