我有一些由Javascript创建的<div>标记,每个标签都有不同的"id“和"class”属性。
一些div标签的样本
<div id="demoid1" onclick="javascript:openDialog(this)" class="demoClass1">demoTag1</div>
<div id="demoid2" onclick="javascript:openDialog(this)" class="demoClass2">demoTag2</div>
<div id="dialog-1" title="Test Case Details">
<P>This my first jQuery UI Dialog!</P>
</div>到目前为止已完成的代码:
function openDialog(ev) {
var docid= ev.id;
$(function () {
$("#dialog-1").dialog({
autoOpen: false,
});
$("#"+docid).click(function () {
$("#dialog-1").dialog("open");
});
});
}请帮帮忙。更新:我有这样的10-15 <div>标签和每一个不同的ID.
我希望这些<div>标签是可点击的,点击它就会弹出一个小的显示窗口。
在需要之前,单击元素的ID,以便从JSON动态获取信息,以便显示信息。
发布于 2016-04-06 07:49:06
首先,你需要一些资源。检查这链接。
https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css https://code.jquery.com/ui/1.11.4/jquery-ui.js
这是一个小提琴,它在那里工作。
试试这个:
$(document).ready(function() {
$(".demoClass").click(function() {
$("#dialog-1").dialog({
resizable: false,
height: 140,
modal: true,
buttons: [{
text: "Yes",
click: function() {
$(this).dialog("close");
},
}, {
text: "No",
click: function() {
$(this).dialog("close");
},
}
],
});
});
});和HTML:
<div id="demoid1" class="demoClass">demoTag1</div>
<div id="demoid2" class="demoClass">demoTag2</div>
<div id="dialog-1" title="Test Case Details">
<P>This my first jQuery UI Dialog!</P>
</div>发布于 2016-04-06 07:40:54
$(document).ready(function() {
$(document).on('click', '#test', function (event) {
alert($('#test').attr('id'));
});
});Try this :).<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="test">ghnhjg</div>
发布于 2016-04-06 07:48:32
考虑到$m是$的jQuery函数对象。
注:这不是一个好主意来处理这个问题。应该使用类选择器调用单击,数值数据应该来自data-numeric属性或类似的内容。
尝试如下:(步骤用注释解释)。
$m("[id^=demoid]").click(function () {
$m(this).prop('id') // this is how you get id of the clicked element
// now I am hoping you are trying to extract the numeric value from the id
//for that you need to do the next line
var numeric = $m(this).prop('id').replace('demoid', '');
$m("#dialog-"+numeric).dialog("open"); // concatinate the numeric value to the #dialog-*
});https://stackoverflow.com/questions/36444424
复制相似问题