簡體   English   中英

jQuery從用戶輸入替換頁面標題

[英]jQuery Replace Page Title from User Input

當我用jQuery按下“開始”按鈕時,我正在嘗試從文本區域替換頁面標題。 我還沒有成功。 我嘗試了replaceWith(),但是沒有用,所以我嘗試了以下方法:

test.html:

<html>
<head>
<title>Test</title
<script src="jQuery.min.js"></script>
<script src="go.js"></script>
<link id="si" rel="shortcut icon" media="all" type="image/x-icon" href="http://www.google.com/favicon.ico" />
<!-- This was for trying to change the icon (also unsuccessful) -->
<link id="i" rel="icon" media="all" type="image/vnd.microsoft.icon" href="http://www.google.com/favicon.ico" />
</head>
<body>
<textarea id="title">Type the new title.>;/textarea>
<!-- Put the favicon switcher here -->
<button onclick="go()">Go</button>
<br />
</body>
</html>`

go.js:

$(function go() {
    var title = document.getElementById('title').value();
    document.title = title;
});    

使用此go函數:

function go() {
    var title = $('#title').val();
    $('title').text(title);
};

沒有jQuery:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <input type="text" onkeyup="updateTitle()" id="tinput">
        <script>
            var title = document.getElementsByTagName('title');
            title = title[0];

            var titleInput = document.getElementById('tinput');

            function updateTitle() {
                title.innerHTML = titleInput.value;
            }
        </script>
    </body>
</html>

編輯:

哦,您在尋找jQuery解決方案。 我只保留答案,也許其他人會使用它:)

$(function() {
   $("button#go").click(function() {
     document.title = $("#title").val();
   });
});

<button id="go">Go</button>

您也可以嘗試以下方法:

go = function () {
    var title = document.getElementById("title").value;
    alert(title);
    document.title = title;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM