简体   繁体   English

<a>使用jquery加载页面后</a>清除<a>href</a>

[英]clearing an <a> href after the page is loaded using jquery

I'm trying to slightly modify a wordpress template. 我正在尝试稍微修改一个wordpress模板。 At the moment a function returns a link to an article, I am trying to replace this link so that instead of diverting you to another page it just brings the article in and loads it. 目前,一个函数返回一篇文章的链接,我试图替换这个链接,这样它就不会将你转移到另一个页面而只是将文章带入并加载它。

To do this I need to reset the anchors href after the page has loaded. 为此,我需要在页面加载后重置锚点href。

This is the bit of code I am interested in: 这是我感兴趣的一些代码:

<?php the_content( __('<img class="readmore" src="/images/readmore.png" title="poo"></img>', 'twentyten' ) ); ?>

returns: 收益:

<a class="more-link" href="http://henryprescott.com/undgraddissintro/#more-12">
<img title="poo" src="/images/readmore.png" class="readmore"></img></a>

However I want to modify this so a script runs instead of taking you to a new page. 但是,我想修改它,以便运行脚本而不是将您带到新页面。

I therefore tried to run this: 因此我试图运行这个:

$(document).ready(function(){  

  $("a.more-link").css("href", "#");
  alert($("a.more-link").css("href"));
}

It does nothing, and the alert returns "undefined". 它什么都不做,警报返回“未定义”。

Where am I going wrong, thanks! 哪里出错了,谢谢!

Use attr() instead of css() . 使用attr()而不是css()

The css method is for getting or setting CSS properties (like margin , color , font-size , etc.). css方法用于获取或设置CSS属性(如margincolorfont-size等)。 The attr method is for getting or setting HTML attributes, like href , src , etc. attr方法用于获取或设置HTML属性,如hrefsrc等。

You are trying to change the attribute with a CSS command, which is wrong. 您正尝试使用CSS命令更改属性,这是错误的。

$(document).ready(function(){  
  $("a.more-link").attr("href", "#");
  alert($("a.more-link").attr("href"));
}

the css() if just for adding inline css, try attr() in this case css()如果仅用于添加内联css,请在这种情况下尝试attr()

jQuery(document).ready(function(){  
jQuery("a.more-link").attr("href", "#");
alert(jQuery("a.more-link").attr("href"));
return false;
}

the return false is to avoid the page reload. 返回false是为了避免页面重新加载。 ;) ;)

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

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