javascript - 如何从 html5 标签字符串中提取属性
问题描述
我有一个字符串:
<input type="text" data-id="1" />
我想允许用户从这个 html5 输入标签字符串中提取属性。
像这样
from('<input type="text" data-id="1" />').extract("type")
我试过这个:
function from(str) {
return {
extract: function (attr) {
return str.split(attr)
}
}
}
但这不起作用。
我怎样才能做到这一点?
仅使用 javascript。
解决方案
这应该可以帮助您:
let x = (new DOMParser()).parseFromString('<input type="text" data-id="1" />', "text/xml");
x.firstChild.getAttribute("type"); // text
推荐阅读
- ros - 如何将 IMU 获得的 3D 角速度数据积分得到线速度的误差最小化?
- php - PHP + SQLite3 根据昵称获取页面
- ios - 使用委托方法到 poptoviewcontroller 的问题 | 斯威夫特 5
- android - 新版本的 android studio 中的 gradle build 失败
- java - Java 解析响应 URLConnection 标头值
- kernel - 通过虚拟地址获取心理地址的位置
- kubernetes - 使用 Minikube 的 Debian VM - 将 VM IP 路由到公开的服务
- c++ - C++中选择和迭代语句内的类型转换过程
- node.js - 在某些条件下使用 xmlbuilder 时,结束标记放错了位置
- python - 如何使用 selenium 在 li 中获取 a 标签