jQuery替换用户输入的页面标题

时间:2013-05-11 23:53:56

标签: javascript jquery html

当我使用jQuery按下“Go”按钮时,我试图从文本区域替换页面标题。我还没有成功。我尝试过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;
});    

4 个答案:

答案 0 :(得分:0)

使用此go功能:

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

答案 1 :(得分:0)

没有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解决方案。我会保留答案,也许其他人会使用它:)

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

你也可以试试这个:

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