检查元素是否合法支持 src 属性或innerHTML

发布于 2024-12-24 01:25:48 字数 421 浏览 2 评论 0 原文

有没有办法检查元素是否可以显示 innerHTML$.html() (例如具有单独结束标记的 elems)或者是一个旨在根据 HTML 规范(例如 )是否有一个 src 属性?我正在寻找快速/可靠的方法来通过 jQuery 或本机 JavaScript 来完成此操作。

编辑:根据 HTML 规范,未设计为具有内部内容的元素称为 void 元素,但也有一些像 这样的元素是完全有效的。

Is there a way to check whether an element can display innerHTML or $.html() (like elems that have a separate closing tag) OR is an element whose is meant to have a src attribute according to the HTML spec such as <img>? I'm looking for fast/reliable way to do this via jQuery or native JavaScript.

Edit: According to the HTML spec, elements not designed to have inner content are called void elements but there are also elements like this <iframe src=url>inner</iframe> that are totally valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨落□心尘 2024-12-31 01:25:48

不幸的是,没有一种万无一失的方法可以做到这一点,因为在 Javascript 中,任何元素都可以具有这些属性。
另外,看起来很奇怪,几乎所有 HTML 元素,包括 ,都有一个 innerHTML 属性,即使它不能真正使用它!

最好的办法是制作一个表格来指定哪些元素具有哪些内容。

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>

        <style>
            </style>

    </head>
    <body>

        <div id="anElementWithInnerHTML"></div>
        <img id="anElementWithInnerSRC" />

        <script>

            var div = document.getElementById("anElementWithInnerHTML");
            console.log(div.innerHTML); //Outputs ""
            console.log(div.src);       //Outputs undefined

            var img = document.getElementById("anElementWithInnerSRC");
            console.log(img.innerHTML); //Outputs "" (weird right?)
            console.log(img.src);       //Outputs ""

            </script>

    </body>
</html>

Unfortunately, there isn't a foolproof way to do this because in Javascript, any element can have those attributes.
Also, as odd at it may seem, almost all HTML elements, including <img>, have an innerHTML attribute, even though it can't really use it!

Your best bet is to make a table that specifies what elements have what.

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>

        <style>
            </style>

    </head>
    <body>

        <div id="anElementWithInnerHTML"></div>
        <img id="anElementWithInnerSRC" />

        <script>

            var div = document.getElementById("anElementWithInnerHTML");
            console.log(div.innerHTML); //Outputs ""
            console.log(div.src);       //Outputs undefined

            var img = document.getElementById("anElementWithInnerSRC");
            console.log(img.innerHTML); //Outputs "" (weird right?)
            console.log(img.src);       //Outputs ""

            </script>

    </body>
</html>
小忆控 2024-12-31 01:25:48

我想你可以

if (typeof element.src !== 'undefined')
if (element.innerHTML ...)

不完全可靠,因为任何元素都可以添加这些属性(JSON)。

(感谢您的修复,小偷)

I suppose you could do

if (typeof element.src !== 'undefined')
if (element.innerHTML ...)

Not totally reliable since any element could add those properties (JSON).

(thanks for the typeof fix, thief)

屌丝范 2024-12-31 01:25:48

我找到了 元素列表(根据规范)允许 src 属性

 audio, embed, iframe, img, input, script, source, track, video

因此这可以按名称进行检查:

function srcAllowed(tag) {
    if ( !tag ) { return false; }
    var tags = ['audio','embed','iframe','img','input','script','source','track','video'];
    return 0 <= $.inArray(tag.toLowerCase(), tags); // boolean
}

这可以获取适当的内容:

function getContent(elem) {
    // @param  elem  is a selected element like $(this)
    // returns empty string if attr() and html() are both are falsey
    return elem.attr('src') || elem.html(); 
}

这甚至更安全:

function getContentSafer(elem) {
    // @param  elem  is a selected element like $(this)
    // returns empty string if attr() and html() are both are falsey
    return srcAllowed(elem.prop('tagName')) ? (elem.attr('src') || elem.html()) : elem.html(); 
}

I found the list of elements that (according to the spec) allow the src attribute:

 audio, embed, iframe, img, input, script, source, track, video

So this works to check by name:

function srcAllowed(tag) {
    if ( !tag ) { return false; }
    var tags = ['audio','embed','iframe','img','input','script','source','track','video'];
    return 0 <= $.inArray(tag.toLowerCase(), tags); // boolean
}

This works for getting the appropriate content:

function getContent(elem) {
    // @param  elem  is a selected element like $(this)
    // returns empty string if attr() and html() are both are falsey
    return elem.attr('src') || elem.html(); 
}

And this is even safer:

function getContentSafer(elem) {
    // @param  elem  is a selected element like $(this)
    // returns empty string if attr() and html() are both are falsey
    return srcAllowed(elem.prop('tagName')) ? (elem.attr('src') || elem.html()) : elem.html(); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文