0

当使用 jQuery 按下“Go”按钮时,我正在尝试从文本区域替换页面标题。我还没有成功。我尝试了replaceWith(),但是没有用,所以我尝试了这个:

测试.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>`

去.js:

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

4 回答 4

0

使用这个 go 函数:

function go() {
    var title = $('#title').val();
    $('title').text(title);
};
于 2013-05-11T23:59:04.883 回答
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 解决方案。我会保留答案,也许其他人会使用它:)

于 2013-05-12T00:00:55.800 回答
0
$(function() {
   $("button#go").click(function() {
     document.title = $("#title").val();
   });
});

<button id="go">Go</button>
于 2013-05-12T00:05:48.213 回答
0

你也可以试试这个:

go = function () {
    var title = document.getElementById("title").value;
    alert(title);
    document.title = title;
}
于 2013-05-12T00:10:52.383 回答