【发布时间】:2011-03-13 07:29:42
【问题描述】:
我试图让我的 wordpress 内部链接出现在内容 div 中,而不是进行正常的页面重新加载。淡入/淡出工作正常,但 div 内容没有改变。 Firebug 没有显示任何错误,并且所有内部链接现在只在页面链接中添加“#/”,我将如何确保使用 jquery 正确添加链接? 提前致谢,
P.S,如果有帮助,我正在关注“css-tricks: ajaxing a wordpress theme”视频,
$(function() {
$(".home li.home").removeClass("home").addClass("current_page_item");
var $mainContent = $("#content"),
URL = '',
siteURL = "http://" + top.location.host.toString(),
$internalLinks = $("a[href^='"+siteURL+"']"),
hash = window.location.hash,
$el, $allLinks = $("a");
if (hash) {
$mainContent.animate({ opacity: "0.1" });
$(".current_page_item").removeClass("current_page_item");
$("a[href="+hash+"]").addClass("current_link").parent().addClass("current_page_item");
hash = hash.substring(1);
URL = hash + " #content";
$mainContent.load(URL, function() {
$mainContent.animate({ opacity: "1" });
});
}
$internalLinks.each(function() {
$(this).attr("href", "#" + this.pathname);
}).click(function() {
$mainContent.animate({ opacity: "0.1" });
$el = $(this);
$(".current_page_item").removeClass("current_page_item");
$allLinks.removeClass("current_link");
URL = $el.attr("href").substring(1);
URL = URL + " #content";
$mainContent.load(URL, function() {
$el.addClass("current_link").parent().addClass("current_page_item");
$mainContent.animate({ opacity: "1" });
});
});
});
【问题讨论】:
-
你试过输出
this.pathname吗?因为我很确定它是未定义的。从而解释了为什么您最终只使用“#”作为链接。 -
看起来不错,路径名没有显示,应该包含在 jquery 中,还是应该使用 var pathname = window.location.pathname?
-
这不包含在 jQuery 中。你应该使用
window.location.href。这将返回当前页面的地址。 -
在这种情况下,我试图抓取一半的内部链接,仅使用 window.location.href 来抓取用户当前所在页面的整个链接,有没有办法将它用于其他页面,例如抓取 /about 和 /contact 页面?
-
我不确定我是否理解您的意思,但是您的意思是链接是否为
http://www.site.com/page。你只想要/page部分?
标签: javascript jquery wordpress dom