简体   繁体   English

从子元素中删除超链接行为

[英]Remove hyperlink behaviour from child elements

I have an anchor tag inside which I append some divs through jQuery. 我有一个锚标记,其中我通过jQuery附加了一些div。 I want that the 'hyperlink' effect of the parent <a> tag should not appear in the appended child elements. 我希望父<a>标记的“超链接”效果不应出现在附加的子元素中。

How can we achieve this with jQuery or in some other way. 我们如何使用jQuery或其他方式实现这一目标。

Here is the fiddle of what I want. 这是我想要的小提琴

UPDATE: Most of the answers tell how to remove the click effect. 更新:大多数答案都告诉您如何删除点击效果。 Isn't there something that can prevent every default behavior of anchor tag from child elements? 是否存在可以阻止子元素的锚标记的每个默认行为的东西?

FIDDLE 小提琴

Ok. 好。 Here is some sample HTML 这是一些示例HTML

<a href="google.com" id="first">
    <div>
        <p>Blah</p>
    </div>
    <div>
        <p>Blah</p>
    </div>
    <div>
        <p>Blah</p>
    </div>
</a>

<a href="google.com" id="second">
    <div>
        <p>Blah 2</p>
    </div>
    <div>
        <p>Blah 2</p>
    </div>
    <div>
        <p>Blah 2</p>
    </div>
</a>

This CSS will remove the underscore, change the font color and the cursor 该CSS将删除下划线,更改字体颜色和光标

div{
    display: inline;
}

#second{
    text-decoration: none;
    color: #000;
    cursor: default;
}

The jquery removes the click event from the children jQuery从子级中删除click事件

$('#second').children().each(function(){
    $(this).click(function(e){
        e.preventDefault();
    });
});

Update : Further to your comments, what you are trying to do is complicated and kind of weird. 更新 :根据您的评论,您正在尝试做的事情很复杂且有点奇怪。 You say for some reason you have to wrap the divs in an achor tag but not have them inherit the properties. 您说出于某种原因,您必须将div包装在achor标记中,但不要让它们继承属性。 You will have to make some trade-off. 您将不得不做出一些权衡。 The trick is to remove the 'href' from the a tag. 诀窍是从a标签中删除“ href” You would not have to write any jQuery/javascript for this. 您不必为此编写任何jQuery / javascript。 As a matter of fact you don't even need any CSS then. 事实上,您甚至不需要任何CSS。 Essentially, remove all the css and jquery from the above and remove the href from the second a. 本质上,从上面删除所有的css和jquery,并从第二个a删除href。

FIDDLE 小提琴

You can use the :first-child Selector. 您可以使用:first-child选择器。 Look at http://api.jquery.com/first-child-selector/ 查看http://api.jquery.com/first-child-selector/

You can remove underline with CSS: 您可以使用CSS删除下划线:

a div {
    text-decoration: none
}

To stop anchor from following its href attribute you need to bind a handler to click event which returns false. 要停止锚点跟随其href属性,您需要将处理程序绑定到返回false的click事件。 In jQuery you can achieve it this way: 在jQuery中,您可以通过以下方式实现:

$('a div').on('click', function() {
    // do whatever you want to do here

    return false;
});

I edited selectors (added div ) after you provided jsfiddle. 提供jsfiddle之后,我编辑了选择器(添加了div )。 Still, there is one more thing. 不过,还有一件事。 According to your fiddle I assume you didn't want "Something" to be wrapped with div. 根据您的小提琴,我认为您不希望div包裹“某些东西”。 It should look like this: jsfiddle 它看起来应该像这样: jsfiddle

You should add an eventlistener to your anchors and prevent the default behaviour: 您应该在锚点上添加一个事件侦听器,并阻止默认行为:

document.getElementsByTagName("a")[0].addEventListener("click", function(e){
    if(e.target.tagName=="DIV") e.preventDefault();
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM