<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>window.onload=function()
{
var aDiv=getElementByAttr('div','data-id','123456');//返回data-id为123456的div集合
for(var i=0;i<aDiv.length;i++)
aDiv[i].style.backgroundColor='#ccc';//
}
function getElementByAttr(tag,attr,value)
{
var aElements=document.getElementsByTagName(tag);
var aEle=[];
for(var i=0;i<aElements.length;i++)
{
if(aElements[i].getAttribute(attr)==value)
aEle.push( aElements[i] );
}
return aEle;
}
</script>
</head><body>
<div data-id="123456" class="box">测试</div>
<div data-id="123456" class="box">测试1</div>
</body>
</html>

解决方案 »

  1.   

    没有id不是还有class么?class不支持的,不是还有tagName吗?用已有的被支持的dom操作方法,获取到更大范围的元素集合(尽量把集合控制到最小),然后到集合中去筛选寻找符合你要求的那个节点便是了。
      

  2.   

    var eles=getElementsByTagName("*");
    for(var i=0,len=eles.length;i<len;i++){
    if(eles[i].getAttribute('data_id')='xxx'){
    return eles[i]
    }
    }
      

  3.   

    所以,还是用 jq 就简单了: $('[data_id])
    你可以看 jq 的源码,看看他是如何做的
      

  4.   

    不好意思,手误 $('[data_id]’)
      

  5.   

    document.querySelectorAll("[data-id='ww']");    加 #1
    这样用可完蛋了。
      

  6.   

    我呢个去,没id不能给他加个ID吗?难不成你是使用别人的网站