简体   繁体   English

<a>用Javascript / JQuery</a>替换` <a>`</a>的正确方法是<a>什么?</a>

[英]What's the proper way to replace `<a>` using Javascript/JQuery?

I want to make HTML elements clickable using Javascript. 我想使用Javascript使HTML元素可点击。 Here's what I have: 这是我所拥有的:

<div class="link">
<a href="http://example.com">
</div>

<script>
$('.link').click(function(){
  if(link=$(this).find('a').attr('href'))
    window.location.href=link;
});
</script>

This works, but I'm wondering if there's a better way to do this. 这可行,但是我想知道是否有更好的方法可以做到这一点。 Thanks! 谢谢!

While your example works, it is neither semantic nor clean. 虽然您的示例有效,但它既不语义也不干净。 You can't right click the div to copy the link location or open in new tab, or middle click it etc. There's nothing stopping you from setting an anchor to display: block to act like a div and then putting all sorts of elements inside the a tag itself. 您不能右键单击div复制链接位置或在新选项卡中打开,或单击鼠标中键等。没有什么阻止您设置锚点来display: block其像div一样动作,然后将各种元素放入其中在a标签本身。 That's the semantic way to do it - let the browser handle the native function of a link. 这就是做到这一点的语义方法-让浏览器处理链接的本机功能。

Starting with HTML5 , you may simply wrap your flow content within your anchor tag. 从HTML5开始 ,您可以简单地将流内容包装在锚标记中。

<a href="http://example.com/">
  <div class="special">
    <img src="http://example.com/news.jpg" alt="Great Picture" />
    <p>Great News! This whole section is clickable!</p>
  </div>
</a>

This allows for the functionality you want without using clunky JavaScript code, and doesn't break browser gestures (middle click for new tab). 这样就可以在不使用笨拙的JavaScript代码的情况下提供所需的功能,并且不会破坏浏览器的手势(单击鼠标中键可看到新标签)。

A few rules however apply in order to stick to the HTML5 standard: 但是,为了遵守HTML5标准,需要遵循一些规则:

  • The parent tag of your <a> must allow for flow content . <a>的父标记必须允许流内容 The anchor tag in HTML5 has a transparent content model, meaning that it adopts the model of its parent. HTML5中的anchor标签具有透明的内容模型,这意味着它采用了其父模型。

  • You may not place any interactive content (eg buttons or other links) within your anchor tag. 您不得在锚标记内放置任何交互式内容(例如按钮或其他链接)。

Note that this works in all browsers, including Internet Explorer 6. However, make sure you set the display style of your <a> tag to either block or inline-block depending on the desired result. 请注意,这适用于所有浏览器,包括Internet Explorer6。但是,请确保根据所需结果将<a>标记的display样式设置为“ block或“ inline-block

如果可以更改html的最佳方法是将div更改为a,则可以将其设置为“ display:block”,因此它是一个类似于div的块元素。

You can also wrap the div in anchor tag ie <a href="#">Div goes here</a> . 您也可以将div包裹在anchor标记中,即<a href="#">Div goes here</a> In this way u get wat u want. 这样,您就可以得到想要的水。 No more styling and javascript needed :) 不再需要样式和JavaScript了:)

If HTML5 is ok you can do something like this: 如果HTML5可以,则可以执行以下操作:

<script type="text/javascript">
$(document).ready(function() {
    $('.link').click(function(e){
        window.location.href = $(this).data('link');
    });
});
</script>
<div class="link" data-link="http://example.com">My Link</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中测试对象是否是jQuery对象的正确/正确方法是什么? - What's the correct/proper way to test if an object is a jQuery object in javascript? 使用Javascript的window.opener的正确方法是什么? - What's the proper way of using Javascript's window.opener? 使用JavaScript,jQuery和AJAX避免达到CPU限制的正确方法是什么? - What is the proper way of using JavaScript, jQuery and AJAX to avoid reaching CPU limits? 在使用AJAX获取html内容后,将侦听器添加到新元素的正确方法是什么? (jQuery,Javascript) - What is a proper way to add listeners to new elements after using AJAX to get the html content? (jQuery, Javascript) 当我使用jquery发送帖子数据时,清理数据的正确方法是什么? - what's the proper way to sanitize data when I'm using jquery to send post data? 使用jQuery或javascript在模板上显示文件树的方式是什么? - What's the way to show file tree on template using jQuery or javascript? 用jquery事件更新javascript中的主类的正确方法是什么? - What is the proper way to update main class in javascript with jquery events? Javascript / jQuery变量,使用var的正确方法是什么? - Javascript/jQuery variable, what is the proper way to use a var? 将jQuery与“ use strict”结合使用的正确方法是什么? - What's the proper way to use jQuery in conjunction with 'use strict'? 使用jQuery检查元素是否具有ID的正确方法是什么? - What's the proper way to check if an element has an ID or not with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM