有一字符串,想用js取出其中键对应的值。

yzyh 2017-03-03 10:18:46
有一字符串如下:
"<a id=\"mytestColId\" href=\"javascript:void(0)\" data=\"tcol1\" columnindex=\"1\" columntype=\"2\">tcol1<font style=\"font-weight: normal;\">(Text)</font></a>"

想用JS取出其中的columnindex和columntype的值(取出来的值应该分别是1和2),请问怎么写?
...全文
347 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复

function getData(input){
	var pattern=/column(?:index|type)="([^"]+)"/ig;
	var data;
	while((data=pattern.exec(input))!=null){
		document.write("数据:"+data[1]+"<br />");	
	}
}
2017-03-03
  • 打赏
  • 举报
回复


var str = "<a id=\"mytestColId\" href=\"javascript:void(0)\" data=\"tcol1\" columnindex=\"1\" columntype=\"2\">tcol1<font style=\"font-weight: normal;\">(Text)</font></a>"
var reg = /(columnindex|columntype)="(\d+)"/ig;
alert(str.match(reg));

qq_35817427 2017-03-03
  • 打赏
  • 举报
回复
var str = "<a id=\"mytestColId\" href=\"javascript:void(0)\" data=\"tcol1\" columnindex=\"1\" columntype=\"2\">tcol1<font style=\"font-weight: normal;\">(Text)</font></a>"; var pattern = /(columnindex|columntype)="[0-9]"/ig; var strings = str.match(pattern); alert(strings[0].charAt(13)); alert(strings[1].charAt(12));
  • 打赏
  • 举报
回复
引用 4 楼 lihe5201234 的回复:
这样也可以: $("#mytestColId").attr("columnindex") 获取到 columnindex的值 $(a).attr("columntype ")获取到 columntype 的值
这是字符串,不是页面
蚂蚁上树 2017-03-03
  • 打赏
  • 举报
回复
这样也可以: $("#mytestColId").attr("columnindex") 获取到 columnindex的值 $(a).attr("columntype ")获取到 columntype 的值

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧