【问题标题】:Context menu - InvokedOn text not getting cloned上下文菜单 - InvokedOn 文本未克隆
【发布时间】:2017-04-03 20:29:46
【问题描述】:

这是我的小提琴 - http://jsfiddle.net/X9tgY/1526/

这行代码不起作用

$(selectedText).clone().insertAfter(selectedText);

您可以通过右键单击 html 文本进行测试,我得到了 invokedOn 文本的 html,但我无法克隆它。

(function ($, window) {

    $.fn.contextMenu = function (settings) {

        return this.each(function () {

            // Open context menu
            $(this).on("contextmenu", function (e) {
                // return native menu if pressing control
                if (e.ctrlKey) return;
                
                //open menu
                var $menu = $(settings.menuSelector)
                    .data("invokedOn", $(e.target))
                    .show()
                    .css({
                        position: "absolute",
                        left: getMenuPosition(e.clientX, 'width', 'scrollLeft'),
                        top: getMenuPosition(e.clientY, 'height', 'scrollTop')
                    })
                    .off('click')
                    .on('click', 'a', function (e) {
                        $menu.hide();
                
                        var $invokedOn = $menu.data("invokedOn");
                        var $selectedMenu = $(e.target);
                        
                        settings.menuSelected.call(this, $invokedOn, $selectedMenu);
                    });
                
                return false;
            });

            //make sure menu closes on any click
            $('body').click(function () {
                $(settings.menuSelector).hide();
            });
        });
        
        function getMenuPosition(mouse, direction, scrollDir) {
            var win = $(window)[direction](),
                scroll = $(window)[scrollDir](),
                menu = $(settings.menuSelector)[direction](),
                position = mouse + scroll;
                        
            // opening menu would pass the side of the page
            if (mouse + menu > win && menu < mouse) 
                position -= menu;
            
            return position;
        }    

    };
})(jQuery, window);




$("#container").contextMenu({
    menuSelector: "#contextMenu",
    menuSelected: function (invokedOn, selectedMenu) {
		
        var msg = "You selected the menu item '" + selectedMenu.text() +
            "' on the value '" + invokedOn.text() + "'";
			var itsId = $(invokedOn).attr('id');
			var selectedText = $(invokedOn).get(0).outerHTML;
			var parentDiv = $(invokedOn).parent();
        alert(selectedText);
		if (selectedMenu = "Clone"){
			alert("inside");
			$(selectedText).clone().insertAfter(selectedText);
		}
    }
});
@import url(http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css)
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div id="container">
<div id="content">content</div>
    <div id="content2">
	<p>This is p </p>
	<h3> This is h3 </h3>
	</div>
    <button id="button">show it</button>
</div>

<ul id="contextMenu" class="dropdown-menu" role="menu" style="display:none" >
    <li><a tabindex="-1" href="#">Clone</a></li>
    <li><a tabindex="-1" href="#">Another action</a></li>
    <li><a tabindex="-1" href="#">Something else here</a></li>
    <li class="divider"></li>
    <li><a tabindex="-1" href="#">Separated link</a></li>
</ul>




<!-- Post Info -->
<div style='position:fixed;bottom:0;left:0;    
            background:lightgray;width:100%;'>
    About this SO Question: <a href='http://stackoverflow.com/q/18666601/1366033'>Use Bootstrap 3 dropdown menu as context menu</a><br/>
<div>

【问题讨论】:

  • 好吧,你有一个脚本错误;P
  • 请打开我的小提琴链接,它在那里完美运行,无法弄清楚为什么它在这里显示错误
  • sn-p 不起作用,因为您在 jquery 之前加载了引导脚本。 (为你修好了

标签: javascript jquery


【解决方案1】:

首先selectedMenu = "Clone" 没有比较任何东西。它将单词 Clone 分配给 selectedMenu 变量。

您应该使用==,并且由于 selectedMenu 是一个 jQuery 对象,您应该再次检查它的文本。所以

 if (selectedMenu.text() == "Clone")

现在,selectedText 还保存用$(invokedOn).get(0).outerHTML 提取的 HTML 字符串

因此您很可能希望克隆该元素并将其插入到自身之后。

$(invokedOn).clone().insertAfter(invokedOn);

(所以你实际上根本不需要提取 html)

(function ($, window) {

    $.fn.contextMenu = function (settings) {

        return this.each(function () {

            // Open context menu
            $(this).on("contextmenu", function (e) {
                // return native menu if pressing control
                if (e.ctrlKey) return;
                
                //open menu
                var $menu = $(settings.menuSelector)
                    .data("invokedOn", $(e.target))
                    .show()
                    .css({
                        position: "absolute",
                        left: getMenuPosition(e.clientX, 'width', 'scrollLeft'),
                        top: getMenuPosition(e.clientY, 'height', 'scrollTop')
                    })
                    .off('click')
                    .on('click', 'a', function (e) {
                        $menu.hide();
                
                        var $invokedOn = $menu.data("invokedOn");
                        var $selectedMenu = $(e.target);
                        
                        settings.menuSelected.call(this, $invokedOn, $selectedMenu);
                    });
                
                return false;
            });

            //make sure menu closes on any click
            $('body').click(function () {
                $(settings.menuSelector).hide();
            });
        });
        
        function getMenuPosition(mouse, direction, scrollDir) {
            var win = $(window)[direction](),
                scroll = $(window)[scrollDir](),
                menu = $(settings.menuSelector)[direction](),
                position = mouse + scroll;
                        
            // opening menu would pass the side of the page
            if (mouse + menu > win && menu < mouse) 
                position -= menu;
            
            return position;
        }    

    };
})(jQuery, window);




$("#container").contextMenu({
    menuSelector: "#contextMenu",
    menuSelected: function (invokedOn, selectedMenu) {
		
        var msg = "You selected the menu item '" + selectedMenu.text() +
            "' on the value '" + invokedOn.text() + "'";
			var itsId = $(invokedOn).attr('id');
			var selectedText = $(invokedOn).get(0).outerHTML;
			var parentDiv = $(invokedOn).parent();
        alert(selectedText);
		if (selectedMenu.text() == "Clone"){
			alert("inside");
			$(invokedOn).clone().insertAfter(invokedOn);
		}
    }
});
@import url(http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css)
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div id="container">
<div id="content">content</div>
    <div id="content2">
	<p>This is p </p>
	<h3> This is h3 </h3>
	</div>
    <button id="button">show it</button>
</div>

<ul id="contextMenu" class="dropdown-menu" role="menu" style="display:none" >
    <li><a tabindex="-1" href="#">Clone</a></li>
    <li><a tabindex="-1" href="#">Another action</a></li>
    <li><a tabindex="-1" href="#">Something else here</a></li>
    <li class="divider"></li>
    <li><a tabindex="-1" href="#">Separated link</a></li>
</ul>




<!-- Post Info -->
<div style='position:fixed;bottom:0;left:0;    
            background:lightgray;width:100%;'>
    About this SO Question: <a href='http://stackoverflow.com/q/18666601/1366033'>Use Bootstrap 3 dropdown menu as context menu</a><br/>
<div>

修复了http://jsfiddle.net/X9tgY/1527/的演示

【讨论】:

  • 感谢它按需工作,但有 1 个问题,我尝试通过单击克隆类似的 html 元素,具体取决于我用来克隆的单击区域,例如 "content" 、 "this is p" 、如果我点击 div,那么它将克隆整个 div “这是 p” 和 “这是 h3” 两者,但现在它只克隆一个特定元素,而不是整个 div,看到这个 - dropbox.com/s/bzfsj17onm6cbkh/… 我怎么能做这个 ?我的旧小提琴代码以防你想看看 - jsfiddle.net/X9tgY/1529
猜你喜欢
  • 2014-03-10
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2015-05-10
  • 2015-12-13
  • 2012-01-22
  • 2019-08-02
  • 1970-01-01
相关资源
最近更新 更多