简体   繁体   English

jQuery将上下文菜单绑定到动态元素ID

[英]Jquery Binding a context menu to dynamic element id

Basic story, I've got a system that handles each div id dynamically. 基本的故事,我有一个系统可以动态处理每个div ID。 I decided to take over the context menu with one of my own, and it works, so long as the id of the element is hardcoded. 我决定用自己的一个接管上下文菜单,并且只要元素的ID是硬编码的,它就可以工作。 I've been trying to write a function that takes the element name from a list of arrays, and its throwing errors left an right. 我一直在尝试编写一个函数,该函数从数组列表中获取元素名称,并且其抛出错误从左到右。

function menuclick()
{
alert("Menuclick Called");
var limen= ["armenu", "ormenu", "prmenu", "apmenu", "auxmenu", "itmenu", "sysmenu"];
var menues= Array();
var men1 = "menu$pf1$topmen$topmen$menul$menuli$";
for (idx=0;idx<6;idx++){
    menues[idx] = "#"+men1+limen[idx];
    $(menues[idx]).bind("contextmenu", this.id, function(e) {
        $('#example-menu').css({
            top: e.pageY+'px',
            left: e.pageX+'px'
        }).show();
return false;
});
    alert(menues[idx]);
}
return;
}

The $(menues[idx]).bind is whats causing the issue, mainly the menues[idx]. $(menues [idx])。bind是导致问题的原因,主要是菜单[idx]。 but i cant figure out why. 但我不知道为什么。 any suggestions? 有什么建议么?

EDIT ** forgot to mention, the error firebug shows is: 编辑**忘了提,萤火虫显示的错误是:

"uncaught exception: Syntax error, unrecognized expression: $pf1$topmen$topmen$menul$menuli$armenu" “未捕获的异常:语法错误,无法识别的表达式:$ pf1 $ topmen $ topmen $ menul $ menuli $ armenu”

EDIT** this uses php to get the div id's from a database. 编辑**这使用php从数据库中获取div id。

ohhh .NET and its silly conventions. .NET及其愚蠢的约定。 You are trying to reference a "name" and not an "id". 您正在尝试引用“名称”而不是“ id”。 Your menu markup should look something like this: 您的菜单标记应如下所示:

<div name="menu$pf1$topmen$topmen$menul$menuli$armenu" id="menu_pf1_topmen_topmen_menul_menuli_armenu"> ... </div>

So, that being said you have two options. 所以,话虽这么说,您有两种选择。

  • 1) Change your dolar signs to underscores (recommended) 1)将美元符号改成下划线(推荐)

var men1 = "menu_pf1_topmen_topmen_menul_menuli_";

  • 2) Change the following line of code: 2)更改以下代码行:

menues[idx] = "#"+men1+limen[idx];

to this: 对此:

menues[idx] = "[name='" + men1 + limen[idx] + "']";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM