简体   繁体   English

任何人都可以解释这行 javascript 吗?

[英]Can anyone explain this line of javascript?

I'm using the DynaTree JavaScript tree and have been modifying it.我正在使用 DynaTree JavaScript 树并且一直在修改它。 Specifically I am trying to add multiple context menus dependent on the type of node.具体来说,我试图根据节点的类型添加多个上下文菜单。

Basically my idea was to have the context menu fire an ajax request to my MVC (2) controller, and it would pass this information as JSON, where it would be used to create a class that can be accessed in c#.基本上我的想法是让上下文菜单向我的 MVC (2) 控制器发出一个 ajax 请求,它会将此信息作为 JSON 传递,在那里它将用于创建一个可以在 c# 中访问的类。

Ive got this working ok, after going through about 500 posts here!在浏览了大约 500 个帖子后,我已经可以正常工作了! Now I need to modify the JS again in order to select an appropriate context menu dependant on type.现在我需要再次修改 JS,以便根据类型选择合适的上下文菜单。

My list looks like我的清单看起来像

<div id="tree">
    <ul>

      <li id="'ID':1,'TYPE':1" title="Look: a tool tip!">item1 with key and tooltip</li>
      <li id="'ID':2,'TYPE':2" class="activate">item2: activated on init</li>
      <li id="'ID':3,'TYPE':3" class="folder">Folder with some children
        <ul>
          <li id="'ID':4,'TYPE':1">Sub-item 3.1</li>
          <li id="'ID':5,'TYPE':1">Sub-item 3.2</li>
        </ul>
      </li>
      <li id="'ID':6,'TYPE':1" class="expanded">Document with some children (expanded on init)
        <ul>
          <li id="'ID':7,'TYPE':1'">Sub-item 4.1</li>
          <li id="'ID':8,'TYPE':1'">Sub-item 4.2</li>
        </ul>
        </li>
      <li id="'ID':9,'TYPE':1" class="lazy folder">Lazy folder</li>
    </ul>
  </div>

And i return the ID and use jQuery.parseJSON to send to MVC.我返回 ID 并使用 jQuery.parseJSON 发送到 MVC。

The JS I dont understand is here:我不明白的JS在这里:

function bindContextMenu(span) {
    // Add context menu to this node:
    debugger;

    $(span).contextMenu({ menu: "myMenu1" }, function (action, el, pos) {
      var node = $.ui.dynatree.getNode(el).toString();
      node = node.replace(/'/g, '\"');
      node = jQuery.parseJSON('{' + node + '}');

      $.ajax({
        type: "POST",
        url: "/TreeView/Click/",
        data: { ID: node.ID, TYPE: node.TYPE },
        error: function (request) { $("#message").html("error"); },
        success: function (result) { $("#message").html("Success - " + result); }
      })
    });
  };

this line - $(span).contextMenu({ menu: "myMenu1" }, function (action, el, pos) {这一行 - $(span).contextMenu({ menu: "myMenu1" }, function (action, el, pos) {

Seems to both set the contextmenu and also fires when a context menu is clicked.似乎既设置了上下文菜单,又在单击上下文菜单时触发。 I'd be happy if someone could just provide some keywords for Google as i dont have a clue.如果有人可以为 Google 提供一些关键字,我会很高兴,因为我不知道。

Thanks in advance提前致谢

.contextMenu looks like a plugin, however without knowing exactly which plugin it is it is hard to give a definitive answer. .contextMenu看起来像一个插件,但是如果不确切知道是哪个插件,就很难给出明确的答案。 However it looks like initialising that plugin takes 2 arguments:但是看起来初始化该插件需要2个参数:

  1. The object containing any setup parameters - this is pretty standard for a plugin包含任何设置参数的object - 这对于插件来说是非常标准的
  2. The function which gets called when the menu is activated (possibly).激活菜单时调用的函数(可能)。

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

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