简体   繁体   English

检查字符串是否是有效的 html 标签。

[英]Check if string is valid html tag.

Is there any Native Javascript Functions to check if html tag exists?是否有任何本机 Javascript 函数来检查 html 标记是否存在?

I mean:我是说:

 var tag = "div";
 alert(isValidTag(tag)) // true;
 alert(isValidTag("foo")) // false;

If there is no native function for that, I will keep my function:如果没有本机功能,我将保留我的功能:

function isValidTag(tagName) {
  var tags = ["div","span","a","link" ... "body"];
  for(var i=0, len = tags.length; i++ < len; ) {
    if(tags[i] == tagName) return true;
  }
  return false;
}

No. JS has nothing HTML specific in it at all, and DOM doesn't add anything like that.没有。JS 根本没有任何特定于 HTML 的东西,DOM 也没有添加任何类似的东西。

How about this:这个怎么样:

tags = 'a b body...'.split(' ');
function isTag(tag) {
  return tags.indexOf(tag.trim().toLowerCase()) > -1;
}
var tags = {a: true, b: true, /*...,*/ body: true};

function isTag(_tag)
{
  return tags[_tag];
}

Thank you, @rsp, for suggesting this simplification谢谢@rsp,建议进行这种简化

This might not be as efficient, I haven't done any time trials on it, but it can still be a good alternative to having to maintain a list of all the possible values yourself.这可能效率不高,我还没有对它进行过任何时间试验,但它仍然是一个很好的替代方法,而不是必须自己维护一个包含所有可能值的列表。

var isHTML = (function() {
  var unknown = '[object HTMLUnknownElement]', overrides = {CANVAS:1,VIDEO:1}; //html5 elements. Some browsers don't support these.
  return function(tag) {
    return overrides[tag = tag.toUpperCase()] || (!overrides.hasOwnProperty(tag) && (overrides[tag] = (document.createElement(tag).toString() !== unknown)));
  };
})();

This method will first check for a cached result, and if there isn't one it will determine a result based on the browser's own document.createElement .此方法将首先检查缓存的结果,如果没有,它将根据浏览器自己的document.createElement确定结果。 If the browser doesn't support it then we can safely assume it isn't an html tag.如果浏览器不支持它,那么我们可以安全地假设它不是 html 标签。

Some sample outputs:一些示例输出:

isHTML('curve'); //false
isHTML('div'); //true
isHTML('DIV'); //true
isHTML('tbody'); //true
isHTML('object'); //true
isHTML('document'); //false
isHTML('html'); //true
isHTML('svg'); //false
isHTML('rect'); //false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 graphql 标签中不允许字符串插值。 (盖茨比) - String interpolation is not allowed in graphql tag. (Gatsby) jQuery检查String是否为HTML标记 - jQuery Check if String is HTML Tag 在HTML中附加文字 <script></script> 标签。 JavaScript问题 - Append text in html <script></script> tag. Javascript issue Java脚本来提醒选择的多选项。 <select> HTMl标签。和标签。不在里面<form>标签体 - java script to alert the mutile option selected ..in<select> HTMl tag. and tag .is not inside <form>tag body Ramda,检查字符串是否有效 - Ramda, check if string is valid 嵌套的 EJS 标签。 不工作 - nested EJS tag. Not working jQuery无法识别我的(格式正确的)HTML标记。 可能的物料清单 - JQuery don't recognized my (well formed) HTML tag. Possible BOM Mustache.js 不会在 html 标签内呈现禁用属性。 但它在其他地方正确呈现 - Mustache.js is not rendering disabled attribute inside html tag. But it is rendering correctly elsewhere 将动态javascript变量附加到HTML标记。 InnerHTML&insertAdjacentHTML都不正确 - Append dynamic javascript variable to HTML tag. InnerHTML & insertAdjacentHTML both incorrect 除非 onload=&quot;init() 在 body 标签内,否则 HTML5 JavaScript Animation 不会播放。为什么? - HTML5 JavaScript Animation doesn't play unless onload="init() is inside the body tag. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM