请问,怎么用jquery或者纯js查找一个元素的属性 <input id="abc...">这里的abc后面还跟着一些字母,但是不确定是哪些字母想要获取这个id属性的值,请问怎么做?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 貌似是这样$("input[id^='abc']").attr("id")多个input要each迭代 不好意思,我说错了<input abc...="属性值">这里的abc后面还跟着一些字母,但是不确定是哪些字母我要的是abc...这个不确定的属性的属性名(比如说abcmmm) var abcmmm = abcmmmdocument.getElementsByTagName('input')[0].getAttribute('abcmmm') 问题是不是有问题啊。html文档模型的属性名可以自己写的吗? 用解XML的方式,然后,采用正则(?)是可以做到的。 键一般都是预知的变化的是值比如你这样写<input id='ip' key='abcxxx:属性值' />这样通过key不就可以拿到整个的了 $('#....').attr('name') 获得名字为name的属性$('#....').attr('name', value) 将属性name的值设置为value 一般常用的标签的属性呀,都是预知的呀,变化的都是属性的值咯除非是你自己定义的标签,自己写的属性才不知道,(自己都不知道,怎么用,呵呵,)要不然的话,属性都就那些,还有什么不知道后面什么字母的呢,wan 以下是JQAPI文档中的一个例子:HTML代码:<img src="test.jpg"/> JQ代码:$("img").attr("src"); 若是根据ID来取,则JQ代码为$("#ID名称").attr("src"); 结果:test.jpg 不是,元素的属性是可以自己加的<input jquery123="555">这个是没有问题的。现在是不知道jquery...具体是什么,可能是jquery123,可能是jquery456,反正是不确定折中的办法是获取上一个元素td的innerHTML值,然后用正则来获取jquery...可能#5楼说的就是这个意思?<input type="text" name="aaa" id="bbb" value="点击" jquery2="mmm" /><script>alert(document.getElementById("bbb").getAttribute("jquery2"));</script> 按1楼的来,可以得到这个input对象,可以考虑呀,但是要重新获得属性名,可以再想办法wan 所有属性名称一条一条对比,发现竟然有100多条。<input type="text" name="aaa" id="bbb" value="点击" jquery2="mmm" /><br><script type="text/javascript">var obj = document.getElementById("bbb");var str = "";var reg = new RegExp("jquery","ig");for(p in obj.attributes) { //str += p + "<br>"; if(reg.test(p)) { alert(p); break; }}//document.write(str);</script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>测试attributes</title><link href="" rel="stylesheet" type="text/css" /></head><body><table border="1" cellspacing="10" style="border:solid 1px"> <tr> <td width="100" rowspan="" > </td> <td width="100"> </td> <td width="100"><input type="text" name="aaa" id="bbb" value="点击" jquery2="mmm" /> </td> </tr> <tr id="d"> <td width="100" id="col"><input type="text" name="aaa" id="bbb" value="点击" jquery2434="mmm" /> </td> <td width="100"> </td> <td width="100"> </td> </tr> <tr> <td width="100"> </td> <td width="100"> </td> <td width="100"> <audio src="someaudio.wav"> 您的浏览器不支持 audio 标签。 </audio> </td> </tr></table></body></html> <script>var obj = document.getElementById("bbb");var str = "";for(p in obj.attributes) { alert(p+"开始") for (m in obj.attributes[p]) { alert(m+":"+obj.attributes[p][m]) }}alert(obj.attributes["length"])/*//下面是运行结果0开始 nodeName:name nodeValue:aaa ownerDocument:[object HTMLDocument] ownerElement:[object HTMLInputElement] firstChild:null lastChild:null childNodes:null nextSibling:null parentNode:null expando:false value:aaa specified:true nodeType:2 name:name previousSibling:null attributes:null 1开始 nodeName:id nodeValue:bbb ownerDocument:[object HTMLDocument] ownerElement:[object HTMLInputElement] firstChild:null lastChild:null childNodes:null nextSibling:null parentNode:null expando:false value:bbb specified:true nodeType:2 name:id previousSibling:null attributes:null 2开始 nodeName:type nodeValue:text ownerDocument:[object HTMLDocument] ownerElement:[object HTMLInputElement] firstChild:null lastChild:null childNodes:null nextSibling:null parentNode:null expando:false value:text specified:true nodeType:2 name:type previousSibling:null attributes:null 3开始 nodeName:jquery2 nodeValue:mmm ownerDocument:[object HTMLDocument] ownerElement:[object HTMLInputElement] firstChild:null lastChild:null childNodes:null nextSibling:null parentNode:null expando:true//这个可能是表明是扩展属性 value:mmm specified:true nodeType:2 name:jquery2 previousSibling:null attributes:null 4开始 nodeName:value nodeValue:点击 ownerDocument:[object HTMLDocument] ownerElement:[object HTMLInputElement] firstChild:null lastChild:null childNodes:null nextSibling:null parentNode:null expando:false value:点击 specified:true nodeType:2 name:value previousSibling:null attributes:null length开始 5*/</script> 如何给highcharts对象赋值?请高手帮忙 请教一个正则表达式的问题,如何匹配空格而不匹配tab javascript 正则表达式^的用法 小问题,给分不多,请大家给看看 如何用JS作出数字自加呀? 求个浏览图片的特效,求大神给个例子或说一下用什么插件可以解决这个问题 能否屏蔽屏幕拷贝键? 高分求解这个系统Bug! 当光标移到图片上时怎把它变为手形? 如何获得鼠标在一个图片上点击的方位 JAVASCRIPT如何在Textarea光标处插入表情? 帮忙改下这个正则
<input abc...="属性值">这里的abc后面还跟着一些字母,但是不确定是哪些字母
我要的是abc...这个不确定的属性的属性名(比如说abcmmm)
键一般都是预知的
变化的是值比如你这样写<input id='ip' key='abcxxx:属性值' />
这样通过key不就可以拿到整个的了
$('#....').attr('name', value) 将属性name的值设置为value
除非是你自己定义的标签,自己写的属性才不知道,(自己都不知道,怎么用,呵呵,)
要不然的话,属性都就那些,还有什么不知道后面什么字母的呢,wan
<img src="test.jpg"/> JQ代码:
$("img").attr("src"); 若是根据ID来取,则JQ代码为
$("#ID名称").attr("src"); 结果:
test.jpg
现在是不知道jquery...具体是什么,可能是jquery123,可能是jquery456,反正是不确定
折中的办法是获取上一个元素td的innerHTML值,然后用正则来获取jquery...
可能#5楼说的就是这个意思?<input type="text" name="aaa" id="bbb" value="点击" jquery2="mmm" />
<script>
alert(document.getElementById("bbb").getAttribute("jquery2"));
</script>
但是要重新获得属性名,可以再想办法wan
<br>
<script type="text/javascript">
var obj = document.getElementById("bbb");
var str = "";
var reg = new RegExp("jquery","ig");
for(p in obj.attributes) {
//str += p + "<br>";
if(reg.test(p)) {
alert(p);
break;
}
}
//document.write(str);
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试attributes</title><link href="" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="1" cellspacing="10" style="border:solid 1px">
<tr>
<td width="100" rowspan="" >
</td>
<td width="100">
</td>
<td width="100"><input type="text" name="aaa" id="bbb" value="点击" jquery2="mmm" />
</td>
</tr>
<tr id="d">
<td width="100" id="col"><input type="text" name="aaa" id="bbb" value="点击" jquery2434="mmm" />
</td>
<td width="100">
</td>
<td width="100">
</td>
</tr>
<tr>
<td width="100">
</td>
<td width="100">
</td>
<td width="100"> <audio src="someaudio.wav">
您的浏览器不支持 audio 标签。
</audio>
</td>
</tr>
</table>
</body>
</html>
<script>var obj = document.getElementById("bbb");
var str = "";
for(p in obj.attributes) {
alert(p+"开始")
for (m in obj.attributes[p])
{
alert(m+":"+obj.attributes[p][m])
}
}
alert(obj.attributes["length"])
/*//下面是运行结果
0开始
nodeName:name
nodeValue:aaa
ownerDocument:[object HTMLDocument]
ownerElement:[object HTMLInputElement]
firstChild:null
lastChild:null
childNodes:null
nextSibling:null
parentNode:null
expando:false
value:aaa
specified:true
nodeType:2
name:name
previousSibling:null
attributes:null
1开始
nodeName:id
nodeValue:bbb
ownerDocument:[object HTMLDocument]
ownerElement:[object HTMLInputElement]
firstChild:null
lastChild:null
childNodes:null
nextSibling:null
parentNode:null
expando:false
value:bbb
specified:true
nodeType:2
name:id
previousSibling:null
attributes:null
2开始
nodeName:type
nodeValue:text
ownerDocument:[object HTMLDocument]
ownerElement:[object HTMLInputElement]
firstChild:null
lastChild:null
childNodes:null
nextSibling:null
parentNode:null
expando:false
value:text
specified:true
nodeType:2
name:type
previousSibling:null
attributes:null
3开始
nodeName:jquery2
nodeValue:mmm
ownerDocument:[object HTMLDocument]
ownerElement:[object HTMLInputElement]
firstChild:null
lastChild:null
childNodes:null
nextSibling:null
parentNode:null
expando:true//这个可能是表明是扩展属性
value:mmm
specified:true
nodeType:2
name:jquery2
previousSibling:null
attributes:null
4开始
nodeName:value
nodeValue:点击
ownerDocument:[object HTMLDocument]
ownerElement:[object HTMLInputElement]
firstChild:null
lastChild:null
childNodes:null
nextSibling:null
parentNode:null
expando:false
value:点击
specified:true
nodeType:2
name:value
previousSibling:null
attributes:null
length开始
5
*/
</script>