将属性从 JQuery-UI-ContextMenu 触发器传递到菜单结构

Passing attributes from a JQuery-UI-ContextMenu trigger to the menu construct

我正在尝试访问作为 JQuery-UI 上下文菜单触发器的 DIV 标签的属性,但我很挣扎。

在学校环境中,我会在 DIV 的文本中包含一个学生的姓名,但我还需要传递一个 ID 号,也许使用 HTML5 数据- pid 属性,或 DIV 的 ID(如有必要)。

我也无法访问,非常感谢一些指导。

我正在使用 ContextMenu 的当代版本 1.12.0 和 JQuery 3.0.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>

$target.id() 不是有效的 jQuery 方法。 使用$target.attr("id") instead. There is also a .data()方法。