【发布时间】:2014-09-11 10:47:50
【问题描述】:
带有 class="front" 的 Div 在 html 页面上被多次克隆,按钮嵌套 (class=poperbtn) 也被克隆,按钮用于打开对话框/弹出窗口 (class="poper") ,例如:如果我有 4 个 div -> class=front,这意味着 4 个按钮 -> class="poperbtn",则每次单击其中一个按钮时,都必须弹出对话框,该怎么做?是否可以?这是一个代码示例。
//对话框-打开按钮
<div class="front">
<input type="button" class="poperbtn" value="push it!" /> </div>
// 对话框的div
<div id="poper"> <h1>here I am </h1></div>
//为了避免使用 id,我以这种方式选择按钮 (id=poperbtn) - 工作正常,我得到了 id="poperbtn" 按钮。
var _btnToDialog = "";
$(".front").live("click", function () {
_btnToDialog = $(this).next().children().eq(0);
});
//对话框Jquery函数-我不确定这段代码..卡在这里..
$(function () {
$("#poper").dialog({
autoOpen: false,
width: 650,
height: 600,
});
$(_btnToDialog).click(function () {
$("#poper").dialog("open");
});
});
});
**根据 cmets,我更改了按钮 - 没有唯一的 Id 类。
【问题讨论】:
-
Id 在 html 页面中应该是唯一的。可以重复上课。将您的按钮 ID 切换为类。
-
更好地使用
data-属性作为 js 钩子 - 留下class用于:css 演示文稿 -
@topless - 好的,如果我将删除 Id 并使用类,我的下一步是什么?
标签: javascript jquery html css