使用.on()在jQuery UI小部件工厂中单击事件

时间:2017-07-03 14:26:36

标签: jquery jquery-ui jquery-ui-tabs jquery-ui-widget-factory

我正在尝试从jQuery UI标签中实现simple manipulation功能。

但是我没有在窗口小部件之外使用代码,而是希望将它放在窗口小部件中。

“添加新标签”工作正常,但我在选项卡内点击关闭图标时遇到问题。我不确定如何使用小部件工厂处理该事件。

这是我到目前为止所做的:

JS:

$.widget("ui.tabs", $.ui.tabs,
{
    _create: function(event, ui)
    {
        this._super();

        this._on($("span.ui-icon-close"), {
        click: function()
        {
            var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
            $( "#" + panelId ).remove();
            this.refresh();
        }});     
    },    
    addTab: function(text, id)
    {
        var tab = "<li><a href='#" + id + "'>" + text + "</a><span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";
        var panel = "<div id='" + id + "'>This is new content! </div>";

        this.tabs.parent().append(tab);
        this.panels.parent().append(panel);

        this.refresh();
    },
});


$( "#tabs" ).tabs();

$("#tabs").tabs("addTab", "New Tab", 3);
$("#tabs").tabs("addTab", "Other Tab", 15);

FIDDLE:https://jsfiddle.net/drpcgx0f/2/

2 个答案:

答案 0 :(得分:0)

如果您只需要一个可以执行魔术的实际点击事件,那么可以通过更改此行来完成:

var tab = "<li><a href='#" + id + "'>" + text + "</a><span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";

到此:

var tab = $("<li>").html("<a href='#" + id + "'>" + text + "</a>");
var close = $("<span>")
              .addClass("ui-icon ui-icon-close")
              .attr("role","presentation")
              .text("Remove Tab")
              .on("click", function() {
                // do whatever you want to do onclick
              });
tab.append(close);

答案 1 :(得分:-1)

请试一试。

$.widget("ui.tabs", $.ui.tabs,
{

    _create: function(event, ui)
    {
      this._super();

		this._on($("span.ui-icon-close"), {
      click: function()
      {
        var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
        $( "#" + panelId ).remove();
        this.refresh();
      }});     
    },    
    addTab: function(text, id)
    {
        var tab = "<li><a href='#" + id + "'>" + text + "</a><span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";
        var panel = "<div id='" + id + "'>This is new content122121212! </div>";
        
        this.tabs.parent().append(tab);
        this.panels.parent().append(panel);
        
        this.refresh();
    },
    removeTab: function(eve)
    {
    $(eve).remove();
// Refresh the tabs widget
		$("#tabs" ).tabs( "refresh" );
    }
});


$( "#tabs" ).tabs();

$("#tabs").tabs("addTab", "New Tab", 3);
$("#tabs").tabs("addTab", "Other Tab", 15);

$(document).on('click', '.ui-icon-close', function (e) {
  $("#tabs").tabs("removeTab", e.target.offsetParent);
});
  #tabs li .ui-icon-close { float: left; margin: 0.4em 0.2em 0 0; cursor: pointer; }
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nunc tincidunt</a></li>
    <li><a href="#tabs-2">Proin dolor</a></li>
    <li><a href="#tabs-3">Aenean lacinia</a></li>
  </ul>
  <div id="tabs-1">
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
  </div>
  <div id="tabs-2">
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  </div>
  <div id="tabs-3">
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
  </div>
</div>