移除片段范围内的节点的所有id属性[英] Remove All id Attributes from nodes in a Range of Fragment

本文是小编为大家收集整理的关于移除片段范围内的节点的所有id属性的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

有没有办法删除范围或片段中每个节点的ID属性?

更新:我终于发现我正在努力的错误是基于A <[脚本]>被包含在一个范围内,因此当Chrome用户执行CTRL+A时,出乎意料的克隆.我的目标是从范围(或DOC片段)中删除<[script]>的任何实例,以使其在克隆时不会复制.

推荐答案

您可能可以使用树wal虫,它在几乎所有范围的浏览器中都可以使用.

function actOnElementsInRange(range, func) {
    function isContainedInRange(el, range) {
        var elRange = range.cloneRange();
        elRange.selectNode(el);
        return range.compareBoundaryPoints(Range.START_TO_START, elRange) <= 0
                && range.compareBoundaryPoints(Range.END_TO_END, elRange) >= 0;
    }

    var rangeStartElement = range.startContainer;
    if (rangeStartElement.nodeType == 3) {
        rangeStartElement = rangeStartElement.parentNode;
    }

    var rangeEndElement = range.endContainer;
    if (rangeEndElement.nodeType == 3) {
        rangeEndElement = rangeEndElement.parentNode;
    }

    var isInRange = function(el) {
        return (el === rangeStartElement || el === rangeEndElement ||
                    isContainedInRange(el, range))
            ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
    };

    var container = range.commonAncestorContainer;
    if (container.nodeType != 1) {
        container = container.parentNode;
    }

    var walker = document.createTreeWalker(document,
        NodeFilter.SHOW_ELEMENT, isInRange, false);

    while (walker.nextNode()) {
        func(walker.currentNode);
    }
}

actOnElementsInRange(range, function(el) {
    el.removeAttribute("id");
});

其他推荐答案

是: http://api.jquery.com/removeattr//p>

本文地址:https://www.itbaoku.cn/post/2091085.html

问题描述

Is there a way to remove the id attribute of every node in a range or fragment?

Update: I finally found out that the bug I'm struggling with is based on a <[script]> being included in a range, and therefore unexpectedly cloned, when a chrome user does a ctrl+a. My goal would be to remove any instance of <[script]> from the range (or doc fragment), such that it is not replicated when cloned.

推荐答案

You may be able to use a TreeWalker, which works in pretty much all the browers that Range works in.

function actOnElementsInRange(range, func) {
    function isContainedInRange(el, range) {
        var elRange = range.cloneRange();
        elRange.selectNode(el);
        return range.compareBoundaryPoints(Range.START_TO_START, elRange) <= 0
                && range.compareBoundaryPoints(Range.END_TO_END, elRange) >= 0;
    }

    var rangeStartElement = range.startContainer;
    if (rangeStartElement.nodeType == 3) {
        rangeStartElement = rangeStartElement.parentNode;
    }

    var rangeEndElement = range.endContainer;
    if (rangeEndElement.nodeType == 3) {
        rangeEndElement = rangeEndElement.parentNode;
    }

    var isInRange = function(el) {
        return (el === rangeStartElement || el === rangeEndElement ||
                    isContainedInRange(el, range))
            ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
    };

    var container = range.commonAncestorContainer;
    if (container.nodeType != 1) {
        container = container.parentNode;
    }

    var walker = document.createTreeWalker(document,
        NodeFilter.SHOW_ELEMENT, isInRange, false);

    while (walker.nextNode()) {
        func(walker.currentNode);
    }
}

actOnElementsInRange(range, function(el) {
    el.removeAttribute("id");
});

其他推荐答案

yes: http://api.jquery.com/removeAttr/