1

求助各位大佬

我这里有一个脚本,在 body 标签上添加一个 ID

这是我看到的结果

<body id="xxx-cat">

使用我在下面使用的脚本

<script>
$(document).ready(function(){
// Edit xxx-cat to match desired category link id:
$('body').attr("id","xxx-cat");
if ($('body[id]')) {
var bid = $("body").attr("id");
// Keep the next command on one line
$('div.droplinebar li a[class='+bid+']').addClass('current');
}
})
</script>

我如何制作 ID (1,2,3,4),因为我有 4 页并且想要它

<body id="1"> for the home page
<body id="2"> for the about
<body id="3"> for the clients
<body id="4"> for the contact

顺便说一下,这是一个 tumblr 自定义页面,这里不能使用 PHP

4

2 回答 2

1

据我了解您的问题,您可以更改脚本,但不能更改页面本身(因为它在 Tumblr 上?)

尝试类似:

$(document).ready(function(){
    var bodyId = '1'; // Set to 1, 2, 3, 4 etc    
    $("body").attr('id', bodyId);
    $('div.droplinebar li a.'+bodyId).addClass('current');
});

但是,正如评论中提到的,您不应该只使用数字作为您的 ID,如果可能,请考虑修改它。

于 2013-02-21T03:00:09.323 回答
1

找到了我的问题的答案

$(function() {
    var pathname = window.location.pathname;
    var getLast = pathname.match(/.*\/(.*)$/)[1];
    var truePath = getLast.replace(".php","");
    if(truePath === '') {
        $('body').attr('id', 'home');
    }
    else {
        $('body').attr('id', truePath);
    }
});

结果

home = <body id="home">
about = <body id="about">
clients = <body id="clients">
contacts = <body id="contacts">
于 2013-02-21T03:32:23.277 回答