[英]How do I make the entire div clickable using plain javascript?
I don't need to write this in jQuery but I'm not versed enough in plain javascript to figure it out. 我不需要用jQuery编写此代码,但是我对普通的javascript并不足够了解。 Chris Coyier wrote a nice explanation of what I'm talking about here .
克里斯·科耶尔(Chris Coyier) 对我在这里所说的话作了很好的解释 。
The reason I want to convert it is because I don't need to include an entire jQuery library for this one piece of code. 我要转换它的原因是因为我不需要为这一段代码包含整个jQuery库。 I can save that extra request by using plain old javascript.
我可以使用普通的旧JavaScript保存该额外请求。
This is the example code that I want to convert: 这是我要转换的示例代码:
$(document).ready(function() {
$(".featured").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});
});
Here's what I've come up with so far: 到目前为止,这是我想出的:
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll("div.feature").click(function(){
window.location=$(this).find("a").setAttribute("href");
return false;
});
});
One thing which isn't correct in this, as far as I know, are the querySelectorAll
, which is looking for just a div element, right? 据我所知,这是不正确的一件事,是
querySelectorAll
,它仅在查找div元素,对吗? The other thing is the $(this)
, which I don't know how to translate into plain javascript. 另一件事是
$(this)
,我不知道该如何将其转换为普通的javascript。
Assuming... 假设...
querySelectorAll
and yet you still use it querySelectorAll
支持,但您仍在使用它 addEventListener
only works for standards compliant browsers addEventListener
仅适用于符合标准的浏览器 I believe you meant: 我相信你的意思是:
//get all a's inside divs that have class "featured"
var feat = document.querySelectorAll("div.featured a"),
featlen = feat.length,
i;
//loop through each
for(i=0;i<featlen;++i){
//add listeners to each
feat[i].addEventListener('click',function(){
window.location = this.href;
},false);
}
Or you can have the <div>
wrapped in <a>
. 或者,您可以将
<div>
包装在<a>
。 No JS required. 不需要JS。 It's perfectly valid HTML and browsers do work as intended despite the rule that inline elements should not contain block elements.
这是完全有效的HTML,尽管内联元素不应包含block元素的规则,但浏览器仍可以按预期工作。 Just make sure to have
display:block
on <a>
as well as adjust its size. 只要确保在
<a>
上具有display:block
并调整其大小即可。
<a href="location">
<div> content </div>
</a>
You can select with this.querySelectorAll(...) : 您可以使用this.querySelectorAll(...)进行选择:
IE8: IE8:
window.onload = function() {
// get all dom elements with class "feature"
var aFeatures = document.querySelectorAll(".feature");
// for each selected element
for (var i = 0; i < aFeatures.length; i++) {
// add click handler
aFeatures[i].onclick = function() {
// get href of first anchor in element and change location
window.location = this.querySelectorAll("a")[0].href;
return false;
};
}
};
IE9 and other current browser: IE9和其他当前浏览器:
document.addEventListener("DOMContentLoaded", function() {
// get all dom elements with class "feature"
var aFeatures = document.querySelectorAll(".feature");
// for each selected element
for (var i = 0; i < aFeatures.length; i++) {
// add click handler
aFeatures[i].addEventListener('click', function() {
// get href of first anchor in element and change location
window.location = this.querySelectorAll("a")[0].href;
return false;
});
}
});
=== UPDATE === ===更新===
For IE7 support you should add following (untested) script before (also see here ): 为了获得IE7支持,您应该在之前添加以下(未经测试的)脚本(另请参见此处 ):
(function(d){d=document,a=d.styleSheets[0]||d.createStyleSheet();d.querySelectorAll=function(e){a.addRule(e,'f:b');for(var l=d.all,b=0,c=[],f=l.length;b<f;b++)l[b].currentStyle.f&&c.push(l[b]);a.removeRule(0);return c}})()
It is possible that it only supports document.querySelectorAll
not element.querySelectorAll
. 可能仅支持
document.querySelectorAll
不支持element.querySelectorAll
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.