简体   繁体   English

使用JQuery从html元素中删除链接

[英]Removing a link from an html element using JQuery

The company I am working for handed me over a messy website ran using templates, so some elements in the website are automatically generated and are active links. 我所工作的公司将我交给一个使用模板运行的凌乱网站,因此该网站中的某些元素是自动生成的,并且是活动链接。

My main problem is that there are some elements on the site are active links that aren't suppose to be links at all. 我的主要问题是网站上的某些元素是活动链接,而这些链接根本不是链接。 I was wondering if there is a way to remove the link from an html element using JQuery and maybe CSS? 我想知道是否有一种方法可以使用JQuery甚至CSS从html元素中删除链接?

this will help me tremendously, thanks in advance. 在此先感谢您,这将极大地帮助我。 I need to remove all href's from class 'slider'. 我需要从“滑块”类中删除所有href。

Here is some of my jquery, can someone please show me how to add it? 这是我的一些jquery,有人可以告诉我如何添加它吗?

<script type="text/javascript">
    $(document).ready(function(){
        $("#productsLink").hover(function(){
            $("#productsMenu").slideDown();
        });
        $("#productsLink, #productsMenu").hover(function(){
            $("#productsLink").css("color","red");
        });
        $(".spacer", this).hover(function(){
            $("#productsLink").css('color', 'white');
            $("#productsMenu").slideUp();
            $("#aboutLink").css('color', 'white');
            $("#aboutMenu").slideUp();
        });
    });
    </script>

如果要从DOM中删除带有类名slider链接:

$('a.slider').remove();

You can remove all href attributes from links with class slider with this code: 您可以使用以下代码从具有类slider链接中删除所有href属性:

$('a.slider').removeAttr('href')

This will keep the content of the elements intact and just disables them as a link. 这将使元素的内容保持完整,并仅禁用它们作为链接。

尝试使用.unwrap()展开滑块,假设所有这些链接都具有类slider ,并且直接将它们包装在锚定标记中。

$('.slider').unwrap()

CSS is no help here, it could only be used to hide elements completely. CSS在这里没有帮助,只能用于完全隐藏元素。

You can use the replaceWith method to turn specific a elements into span elements. 您可以使用replaceWith方法将特定a元素转换为span元素。 Example: 例:

$('a.something, a.someother').replaceWith(function(){
  return $('<span/>').text($(this).text());
});

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

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