点击文本框事件得到冒泡

时间:2012-07-23 08:06:12

标签: javascript jquery event-bubbling

这是我的HTML结构。

<div id="dvHirearachy" class="MarginTB10">
    <span>
        <label>Hierarchy Names</label><br />
        <input type="text" name="txtHierarchy" />
        <a id="ancRemove" href="#">Remove</a>
    </span><br />
    <a id="ancNew" href="#">New Hierarchy Name</a>
</div>

点击锚标记“ancNew”后,我再次生成标记中提到的完整span标记。

问题在于单击文本框,还会生成跨度结构。同样的问题我面对点击“ancRemove”因为我试图阻止事件冒泡,它已经为此工作但不适用于文本框。

我的剧本。

 $(document).ready(function () {

            $("#ancRemove").click(function (e) {
                RemoveHierarchy(this, e);
            });

            $("#ancNew").click(function (e) {
                generateNewHierarchy(e);
            });
});
 function generateNewHierarchy(e) {
            if (window.event) {
                var e = window.event;
                e.cancelBubble = true;
            } else {
                e.preventDefault();
                e.stopPropagation();
                var container = createElements('span', null);
                $(container).append(createElements('input', 'text'));
                $(container).append(createElements('a', null));
                $(container).append("<br/>").prependTo("#ancNew");
                $(container).children('input[type=text]').focus();
            }
        }

        function createElements(elem,type) {
            var newElem = document.createElement(elem);

            if (type != null) {
                newElem.type = "input";
                newElem.name = "txtHierarchy";
                $(newElem).addClass('width_medium');
            }

            if (elem == "a") {
                newElem.innerHTML = "Remove";
                $(newElem).click(function (e) {
                    RemoveHierarchy(this,e);
                });
            }
            return newElem;
        }

        function RemoveHierarchy(crntElem, e) {
            e.stopPropagation();
            $(crntElem).parents("span:first").remove();
        }

避免这种情况的方法是什么。

1 个答案:

答案 0 :(得分:1)

检查这个jsfiddle:

http://jsfiddle.net/xemhK/

问题是prepandTo语句,它正在准备#ancNew锚标记中的元素,这就是为什么所有文本框和删除锚点都在传播#ancNew的click事件,并且它正在调用generateNewHierarchy()函数。

$(container).append("<br/>").prepandTo("#ancNew");更改为$(container).append("<br/>").insertBefore("#ancNew");

function generateNewHierarchy(e) {
    if (window.event) {
        var e = window.event;
        e.cancelBubble = true;
    } else {
        e.preventDefault();
        e.stopPropagation();
        var container = createElements('span', null);
        $(container).append(createElements('input', 'text'));
        $(container).append(createElements('a', null));

        //$(container).append("<br/>").prepandTo("#ancNew");
        $(container).append("<br/>").insertBefore("#ancNew");

        $(container).children('input[type=text]').focus();
    }
}

和createElements

if (elem == "a") {
    newElem.innerHTML = "Remove";
    $(newElem).attr("href","#").click(function (e) {
        RemoveHierarchy(this,e);
    });
}