Событие клика JQuery запускает неправильный элемент? - PullRequest
0 голосов
/ 22 февраля 2019

У меня проблема с тем, что приведенный ниже код запускается не на том элементе, код ниже находится внутри функции _create виджета jquery с именем tag, но когда я нажимаю кнопку x, как показано на рисунке ниже, он удаляеттег "goo" вместо "yoo", но я нажал на "yoo"

https://gyazo.com/c255d8c136624c235a3af0de4ee40fff

if(this.options.removable)
{

        this.removeButton = $("<span class='close glossy'>x</span>").appendTo(this.element);
        console.log(this.removeButton.parent().text());

        this.removeButton.click(function(e){
            alert(self.element.text());
            self._destroy();

            e.preventDefault();
        });
}

, и это итоговый HTML-код для справки

изображение результирующего html, отображаемого в инспекторе

Заранее спасибо.

это полный код JS для виджета

$.widget( "search.tagify", {

options: {
    followerCount: 30,
    description: 

    `a Javascript library, consider also adding the Javascript tag. jQuery is a
     popular cross-browser JavaScript library that facilitates
      Document Object Model (DOM) traversal, 
     event handling…,`,

    name: "tag name goes here",
    removable: false
},

_destroy: function()
{
    //Tips.remove(this.element);
    this.element.remove();
},

_create: function() {
    self = this;

    this.element
        .addClass( "tag" )
        .text( this.options.name )
        .protipSet({
            title: this.options.description,
            position: "bottom",
            size: "small",
            skin: "square",
            classes: "tag-tooltip",
        })

    if(this.options.removable)
    {

        this.removeButton = $("<span class='close glossy'>x</span>").appendTo(this.element);
        console.log(this.removeButton.parent().text());

        this.removeButton.click(function(e){
            alert(self.element.text());
            self._destroy();

            e.preventDefault();
        });
    }

},

});

1 Ответ

0 голосов
/ 22 февраля 2019

Пожалуйста, проверьте следующий код ..

if(this.options.removable)
{

    this.removeButton = $("<span class='close glossy'>x</span>").appendTo(this.element);
    console.log(this.removeButton.parent().text());

    this.removeButton.click(function(e){
        alert(self.element.text());
        //here is the change you can access the targeted element like this as well
        $(e.target).parent('.tag').remove();

        e.preventDefault();
    });
}

Я внес изменения в код, пожалуйста, проверьте, я показал другой механизм для выбора цели

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...