简体   繁体   English

编辑 <title>页面上使用&lt;span&gt;分区的数据

[英]Edit <title> data using a <span> division on the page

<html>
    <head>
        <title></title>
        <script src="js.js"></script>
        <script>
            var name = $("#postheading").text();
            $(document).ready(function(){
                $("title").text(name)    
            }); 
        </script>
    </head>
    <body>
        <span id="postheading">THE PAGE TITLE INFO or POST HEADING</span>
    </body>
</html>

I hope my question is clear from the code above. 我希望上面的代码能清楚我的问题。 But unfortunately it isn't working. 但是不幸的是它没有用。 I have many pages and want to have the <title> info from heading of the post (for my blog), ie my span element with id="postheading" . 我有很多页面,并且想从帖子标题(对于我的博客)获得<title>信息,即我的span元素具有id="postheading"

Can you try this, 你可以试试这个吗

    $(document).ready(function(){           
        $("title").text($("#postheading").text());  
    }); 

Firstly, you need to retrieve the #postheading value within the document.ready block. 首先,您需要在document.ready块中检索#postheading值。 Secondly, you can use the native document.title to set the page title. 其次,您可以使用本机document.title设置页面标题。 Try this: 尝试这个:

$(document).ready(function() {
    var name = $("#postheading").text();
    document.title = name;
}); 

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

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