<a href="http://www。baidu。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。360。cn/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。qq。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。taobao。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。uuyuuy。com/xxx/xxx/xxx.html">xxx</a>
如果有id或class直接
var href = document.getElementById("xxx").href;
可是现在没有id和class。。。
那么要怎么写才能取到 带www。baidu。com的href呢?
求详细代码。望高手指点指点。。。谢谢。
<body>
<a href="http://www。baidu。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。360。cn/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。qq。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。taobao。com/xxx/xxx/xxx.html">xxx</a>
<a href="http://www。uuyuuy。com/xxx/xxx/xxx.html">xxx</a>
<script>
let alist=document.getElementsByTagName('a');
for(var i=0;i<alist.length;i++){
console.log(alist[i].getAttribute('href'))
}
</script>
</body>
你可以用function函数来实现传参例如:
function fn(a,b){
var c=a+b;
return c;
}
var c1=fn(1,2);
console.log(c1);//打印出c的值为3
这就是function函数的传参。
var href = document.querySelector("a[href*=www。baidu。com]").href;
<body>
<a href="www.baidu.com1"></a>
<a href="www.baidu.com2"></a>
<a href="www.baidu.com3"></a>
<a href="www.baidu.com4"></a>
<a href="www.baidu.com5"></a>
</body>
<script src="../js/jquery-1.11.0.min.js"></script>
<script>
var a = $('a').eq(0).attr('href');
console.log(a)
</script>