简体   繁体   English

在javascript中将函数写为字符串

[英]Write function as a string in javascript

I would like to create a context driven menu. 我想创建一个上下文驱动的菜单。 This menu comes when the user clicks on any of the nodes in the tree. 当用户单击树中的任何节点时,会出现此菜单。 Each node has a class "treedropdownmenu". 每个节点都有一个“treedropdownmenu”类。 On node clicked, the context driven menu should open up. 在单击节点上,应打开上下文驱动的菜单。 I am passing a method "_deleteClick" with the Delete menu option. 我正在使用“删除”菜单选项传递方法“_deleteClick”。 But it is throwing me an error : "_deleteClick" menu not found. 但它给我一个错误:找不到“_deleteClick”菜单。

I have following fine of code in my widget : 我的小部件中有以下代码:

$(".treedropdownmenu").live("click", function (event) {
    var pos;
    if(($(window).height() - event.pageY) < 80) {
        pos = {
            left: event.pageX + 20,
            top: event.pageY - 60
        };
    } else {
        pos = {
            left: event.pageX + 20,
            top: event.pageY + 20
        };
    }
    if(ko.dataFor(this).nodeId() && ko.dataFor(this).nodeId() !== 0) {
        var item = ko.dataFor(this);
        var strHtml = "<a href='#' onclick='_deleteClick(item)'>Delete:</a> " + "<br/>" + "<b>Create Date:</b>" + "<br/>" + "<b>Exposed Party Name:</b>" + "<br/>" + "<b>Portfolio Type:</b>" + "<br/>" + "<b>Owner:</b>";
        $("#dataManagerMenuItem1234").show().offset(pos).html(strHtml);
    }
});

The delete menu I have is : 我的删除菜单是:

function _deleteClick(item) {
    alert("delete clicked");
}

Can anyone let me know where am I going wrong? 谁能让我知道我哪里错了?

Yeah, I don't think that will work. 是的,我认为这不会起作用。 Try this: 尝试这个:

var strHtml = "<a href='#'>Delete:</a> " + "<br/>" + "<b>Create Date:</b>" + "<br/>" + "<b>Exposed Party Name:</b>" + "<br/>" + "<b>Portfolio Type:</b>" + "<br/>" + "<b>Owner:</b>";
 $("#dataManagerMenuItem1234").show().offset(pos).html(strHtml).find('a').click(function() { _deleteClick(item); });

This is kindof a clumsy way of adding an event handler to the link, especially since you're creating it programmatically anyways. 这是一种向链接添加事件处理程序的笨拙方式,特别是因为您无论如何都是以编程方式创建它。 Why not add the click handler dynamically, like so? 为什么不动态添加点击处理程序,如此?

var item = ko.dataFor(this);
var deleteLink = $('<a>', {
    href: '#',
    click: function() {
        _deleteClick(item);
    },
    text: 'Delete:'
});
$("#...").show().offset(pos).append(deleteLink);

Do not addthe event handler in a string, that is bad practice. 不要在字符串中添加事件处理程序,这是不好的做法。

var strHtml = "<a href='#'>foo</a>;
var lnk = jQuery(strHtml);
lnk.on("click", function(){ _deleteClick(item); });
$("#dataManagerMenuItem1234").show().offset(pos).append(lnk);

You haven't posted all your code here, but I assume you have defined the function _deleteClick in a local scope, and your onclick handler cannot access it from the global scope. 您尚未在此处发布所有代码,但我假设您已在本地范围内定义了函数_deleteClick ,并且您的onclick处理程序无法从全局范围访问它。 This is one of the reasons why you shouldn't use onclick attributes to attach events to DOM elements! 这是您不应该使用onclick属性将事件附加到DOM元素的原因之一! Your onclick attribute, written out as a string, is not going to execute in the right context. 您的onclick属性(以字符串形式写出)不会在正确的上下文中执行。 Use jQuery to subscribe instead. 使用jQuery来订阅。

It looks like you're using KnockoutJS. 看起来你正在使用KnockoutJS。 An even better way to do this would be using a Knockout template to write out your menu. 更好的方法是使用Knockout模板写出你的菜单。

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

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