【问题标题】:jQuery Replace Page Title from User InputjQuery 从用户输入中替换页面标题
【发布时间】:2013-05-12 11:06:06
【问题描述】:

当使用 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) 个空格作为代码块,并在行尾乘以 (2+) 个空格作为换行符。
  • 使用谷歌怎么样?通过简单的良好搜索,这就是我发现的:stackoverflow.com/questions/987967/…
  • 只需删除 $( 和 );来自 go.js &try
  • 我正在寻找同样适用于更新网站图标的解决方案。

标签: javascript jquery html


【解决方案1】:

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

【讨论】:

  • 谢谢!这很有用,虽然 jQuery 解决方案会很好,所以我也可以使用它来更改网站图标。
  • 似乎没有其他人在发帖,这种方式效果最好(尽管其他一些方式也可以),所以我将其标记为答案。
【解决方案2】:
$(function() {
   $("button#go").click(function() {
     document.title = $("#title").val();
   });
});

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

【讨论】:

    【解决方案3】:

    你也可以试试这个:

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

    【讨论】:

      【解决方案4】:

      使用这个 go 函数:

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

      【讨论】:

      • 点击后不会改变。
      • 抱歉,已编辑...函数在文档就绪时执行错误
      猜你喜欢
      • 2022-08-15
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      相关资源
      最近更新 更多