[英]make div clickable with jquery
jsfiddle: http://jsfiddle.net/FgZnK/1/ jsfiddle: http : //jsfiddle.net/FgZnK/1/
Clicking on the box goes to a page not found. 单击该框转到未找到的页面。 How can I make this work?
我该如何进行这项工作?
HTML 的HTML
<div class="myBox"></div>
jQuery jQuery的
$(".myBox").click(function(){
window.location=$(this).attr("http://google.com");
return false;
});
This is the correct code: 这是正确的代码:
$(".myBox").click(function() {
window.location = "http://google.com";
});
http://jsfiddle.net/FgZnK/2/ http://jsfiddle.net/FgZnK/2/
HTML 的HTML
<div class="myBox" data-href="http://google.com/">
</div>
JS JS
$(".myBox").click(function(){
window.location = $(this).attr("data-href");
return false;
});
In this chunk here: 在此块中:
$(".myBox").click(function(){
window.location=$(this).attr("http://google.com");
return false;
});
You're actually trying to read the non-existent attribute named http://google.com
. 您实际上是在尝试读取名称为
http://google.com
的不存在的属性。
Instead, just use: 相反,只需使用:
$(".myBox").click(function(){
window.location = 'http://google.com';
});
If instead you want the actual destination to be in the mark up rather than the script, use: 相反,如果您希望实际目的地位于标记中而不是脚本中,请使用:
$(".myBox").click(function(){
window.location = $(this).attr('href');
});
and put an href
attribute on your DIV, just like you would on an A link. 并在DIV上放置
href
属性,就像在A链接上一样。
There's no need for return false
in any of these handlers because a DIV doesn't have a default action that needs preventing, unlike a normal link. 与常规链接不同,DIV不需要默认操作,因此不需要在这些处理程序中
return false
。
window.location="http://google.com";
$(".myBox").click(function(){
window.location.href="http://google.com";
return false;
});
This is the faulty line : 这是错误的行:
window.location=$(this).attr("http://google.com");
This doesn't make any sense, you're trying to change the location to the value of the attribute named "http://google.com" of your div. 这没有任何意义,您正在尝试将位置更改为div的名为“ http://google.com”的属性的值。
Just correct it like this : 像这样纠正它:
$(".myBox").click(function(){
window.location= "http://google.com";
return false;
});
Open in new window.... 在新窗口中打开....
$("#whatever").click(function() {
window.open(
'https://www.example.com',
'_blank' // open in new window
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.