我有一个div(A),在另一个div(B)里面,然后我有我的链接。我想要发生的是当我们点击A时,div(B)内的链接有效。
我成功地与父母一方做了,但没有2 ...
$(".block").mouseover(function() {
if ($(".block .button").length) {
$(this).css("cursor", "pointer").find(".invisible.button").css("text-decoration", "none");
}
}).mouseout(function(e) {
e.preventDefault();
e.stopPropagation();
$(this).find(".invisible.button").css("text-decoration", "none");
}).click(function(e) {
document.location.href = $(this).find(".invisible.button").attr("href");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="wrap">
<div class="wrap-in block">
<p>content</p>
<a class="invisible button" href="mylink"> go</a>
</div>
</div>
点击“.wrap”div时,我无法找到让链接工作的方法,如果有人可以提供帮助的话:)
还有一个信息: 如果“换行”没有链接,我还有一个类:
重点是,如果我这样做:
$(".wrap").mouseover(function() {
if ($(".block .button").length) {
$(this).css("cursor", "pointer").find(".invisible.button").css("text-decoration", "none");
}
}).mouseout(function(e) {
e.preventDefault();
e.stopPropagation();
$(this).find(".invisible.button").css("text-decoration", "none");
}).click(function(e) {
document.location.href = $(this).find(".invisible.button").attr("href");
});
它有效,但它适用于div 没有任何链接......
我编码以阻止“nolink”
$('.nolink').on('click', function(e){
e.preventDefault();
e.stopImmediatePropagation();
});
答案 0 :(得分:1)
如果锚是永久隐藏的,那么有几种解决方案会更“正确”。
喜欢将包装纸变成锚。如果容器总是会导致页面重定向,为什么不将整个div作为链接?
此外,您可以在包装器上的数据标记中隐藏href地址。
<div class="wrapper" data-href="myUrl">(...)</div>
$(".wrap").on("click", function() {
window.location.replace($(this).data("href"));
});
答案 1 :(得分:0)
你是否想要做这样的事情:
$(".wrap").on("click", function() {
window.location = $(this).children("div.wrap-in").children("a.invisible").attr("href");
});
答案 2 :(得分:0)
好的,非常感谢你的帮助!我找到了一种方法来做我真正想做的事情。因为我只需要在包含链接的div上添加链接和光标。
这是我的代码清理(我希望),它的工作原理,没有一个无法解释的链接
$(".wrap").mouseover(function(){
if($(this).find("a.invisible").length){
$(this).css("cursor","pointer");
}
}).mouseout(function(e){
$(this).css("cursor","auto");
});
$(".wrap").on("click", function() {
if($(this).find("a.invisible").length){
btn = $(this).find("a.invisible");
window.location = $(this).find("a.invisible").attr("href");
}
});