English 中文(简体)
为什么html能够读到这一javascript功能?
原标题:Why html can´t read this javascript function?

I am trying to recreate the google main page however my problem is with the I am feeling lucky button when I press the I am feeling lucky button the function luck() is not being recognized.

错误:

Uncaught TypeError Type错误: luck is not a function

我的法典:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Search</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="combinados.js"></script>
        <script type="text/javascript">
            function luck() {
                var query = document.querySelector(".search").value;
                window.location.href = "https://www.google.com/search?q=" + encodeURIComponent(query) + "&btnI";
            };
         </script>

    </head>
    <body >
        <form class="bar" action="https://www.google.com/search">
            <input class="search" type="text" name="q">
            <input id ="search" type="submit" value="Google Search">
            <input id ="luck" type="button" value="I am feeling lucky" onclick="luck()">
            
        </form>
        <ul id="links">
            <li><a href="imagesearch.html">Image Search </a></li>
            <br>
            <li><a href="advancedsearch.html">Advanced Search </a></li>
        </ul>
    </body>
</html>

Why doesn t this code work?

最佳回答

在你的法典中,你有<条码>和>;投入和>;和功能名称相同。 因此,当 but子被点击时,浏览器试图执行<条码>luck()功能,但有一个与<条码>luck<>/代码”相近的超文本元素,造成混乱。

为了确定这一点,你可以更改你的职能名称或<input>

这方面的一个例子是,我将职能名称改为feelingLucky()。

<input id="luck" type="button" value="I am feeling lucky" onclick="feelingLucky()">
function feelingLucky() {
    var query = document.querySelector(".search").value;
    window.location.href = "https://www.google.com/search?q=" + encodeURIComponent(query) + "&btnI";
};
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签