我试过找一个类似的例子并用它来回答我的问题,但我似乎无法让它工作,所以如果这听起来与其他问题类似,请道歉。
基本上,我正在使用Terminal Four的Site Manager CMS系统来构建我的网站。此工具允许您生成在整个站点中使用的导航元素。
我需要添加一个JS的自定义位来将这些链接附加到锚点。
生成的链接类似于:
<ul id="tab-menu">
<li><a href="/section/page">test link, can i rewrite and add an anchor!!!</a></li>
</ul>
我可以编辑链接的css属性,但我无法弄清楚如何添加锚。
我使用的JQuery如下:
<script type="text/javascript" src="http://jquery.com/src/jquery-latest.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// everything goes here
$("#tab-menu").children("li").each(function() {
$(this).children("a").css({color:"red"});
});
});
</script>
提前感谢您的帮助。
水稻
答案 0 :(得分:3)
有点重复: How to change the href for a hyperlink using jQuery
只需复制旧的href并添加锚点并将其粘贴回来
var link = $(this).children("a").attr("href");
$(this).children("a").attr("href", link+ "your own stuff");
答案 1 :(得分:1)
一个不错的基于jQuery的方法是使用.get(index)方法来访问each()函数中的原始DOM元素。然后,您可以访问JavaScript链接对象,该对象具有一个名为“hash”的属性,该属性表示URL的锚点部分。所以稍微修改你的代码:
<script type="text/javascript">
$(document).ready(function(){
// everything goes here
$("#tab-menu").children("li").children("a").each(function() {
$(this).css({color:"red"}).get(0).hash = "boom";
});
});
将“#tab_menu li”中的所有链接更改为红色,并将“#boom”添加到结尾。
希望这有帮助!
答案 2 :(得分:0)
我现在可以使用以下内容来定位html:
$(this).children("a").html("it works");
我认为:
$(this).children("a").href("something");
会编辑href,但我错了。
水稻
答案 3 :(得分:0)
我不确定答案,我不尝试
$("#tab-menu").children("li").children("a").each(function() {
// $(this).css({color:"red"}).get(0).hash = "boom";
var temp_url = $(this).href +'#an_anchor';//or var temp_url = $(this).attr('href');
$(this).attr('href', temp_url);
});