【问题标题】:Getting a link from another div从另一个 div 获取链接
【发布时间】:2014-05-08 19:20:53
【问题描述】:

我有

<div class="box box1">
 <div class="content">

  <img a href="http://www.example.com" />
    <img src="https://www.google.ie/images/srpr/logo11w.png"</a>
 </div>
</div>

我想要做的是让盒子的整个 div 都可以点击。

我在 StackOverFlow 中环顾四周,看到了一些很棒的示例,但我尝试了它们,但无法让它们工作。

例如:

$div.next('div.box2').find('a').attr('href', $div.find('a').attr('href'));

任何关于如何在 jquery 中执行此操作的帮助都会很棒

【问题讨论】:

  • 什么是$div?顺便说一句,您的 HTML 无效。您从未关闭过第二个 img 并且 img 没有 href 属性,而是 src
  • 你想要什么点击?
  • 即内容div div.content
  • 在这种情况下,$div.next('div.box2') 将找不到任何东西。因为你的盒子里没有那个类的 div。
  • 当我点击 div 框时,它应该链接到 div 内容中的 href="example"

标签: javascript jquery html css


【解决方案1】:
$(".box").click(function(){
     window.location=$(".whereeveryourelookingfor").find("a").attr("href"); 
     return false;
});

你可以让鼠标指针看起来也像 href 上的一样,使用下面的 css

.box{ cursor:pointer; }

http://jsfiddle.net/r9Xhf/

【讨论】:

  • 这看起来不错,现在试试
【解决方案2】:
<div class="box box1" style="cursor: pointer">
<div class="content">
 <a href="example">
  <img a href="http://www.example.com" />
    <img src="https://www.google.ie/images/srpr/logo11w.png"</a>
</div></div>

JS

$('.box').click(function(){
    window.location.href = "http://www.example.com";
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多