我正在尝试访问充当 JQuery-UI 上下文菜单触发器的 DIV 标记上的属性,但我正在苦苦挣扎。
在学校环境中,我将在 DIV 的文本中包含一个学生的姓名,但我还需要传递一个 ID 号,可能使用 HTML5 数据 pid 权限,或者必要时使用 DIV 的 ID。
我也无法访问,非常感谢一些指导。
我正在使用 ContextMenu 和 JQuery 3.0.0 的当代版本 1.12.0。
请看下面我的代码。提前致谢。
<div id="TheIDIWantToAccess" data-pid="AnotherWantedVariable" class="hasStudentContextMenu"><p>The inner text which is showing fine using $target.text()</p></div>
<script type="text/javascript">
$( document ).ready(function() {
$("#studentContextMenus").contextmenu({
delegate: ".hasStudentContextMenu",
preventContextMenuForPopup: true,
menu: []
,beforeOpen: function(event, ui) {
var $menu = ui.menu,
$target = ui.target;
$(this).contextmenu("replaceMenu",
[
{title: "<b>" + $target.text() + "</b>"}
,{title: "Award"
,children: [
{title: "1_Point", action: function(event, ui) { alert("1 point awarded: " + $target.text() + " (" + $target.id() + ")");}},
{title: "2_Points", action: function(event, ui) { alert();} },
]
}
]
);
}
,select: function(event, ui) {
//alert("select " + ui.target.attr("id"));
}
});
});
</script>