简体   繁体   English

无法使JQuery工作

[英]Can't make JQuery work

Tools: Visual Studio 2010 工具: Visual Studio 2010

I'm trying to learn jQuery, but I cant make this example work even after trying for 2 hours. 我正在尝试学习jQuery,但是即使尝试了2个小时,也无法使此示例正常工作。 I know it's quite simple but I don't know what I'm missing. 我知道这很简单,但是我不知道自己缺少什么。

JQuery is defined here, inside the <head> tag: jQuery在<head>标记内定义:

<head runat="server">
<title></title>
<script src="Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("button").click(function () {
            $("#divTest1").hide();
            $("p").hide();
        });
    });
</script>
</head>

<body>
<form id="form1" runat="server">
<div>
    <div id="divTest1">
        Itz too much I cant make it work...!
    </div>
    <h2>
        This is a heading</h2>
    <p>
        This is a paragraph.</p>
    <p>
        This is another paragraph.</p>
    <button>
        Click me</button>
</div>
</form>
</body>

Updates: 更新:

  • I have tried this $(document).ready(function () { alert("Hello"); }); 我已经试过$(document).ready(function(){alert(“ Hello”);}); And added breakpoint in firebug/Script,after page load firebug got into the jquery library and the went out of it,but it didn't showed any popup 并在firebug / Script中添加了断点,在页面加载后,firebug进入了jquery库并退出了jquery库,但是它没有显示任何弹出窗口

  • I just changed from "jquery-1.7.1-vsdoc.js" to this "jquery-1.7.1.js" and used simple script to check if itz working $(document).ready( function(){ alert("Hello"); } ); 我只是从“ jquery-1.7.1-vsdoc.js”更改为“ jquery-1.7.1.js”,并使用简单的脚本来检查itz是否在$(document).ready( function(){ alert("Hello"); } ); ,jquery is working now. ,jquery现在正在工作。

As pointed out by Matt Grande, you're not actually including the jQuery library in your page. 正如Matt Grande指出的那样,您实际上并未在页面中包括jQuery库。 You need to add the following to the head : 您需要在head添加以下内容:

<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>

The vsdoc version is just to add Intellisense support. vsdoc版本仅用于添加Intellisense支持。

Also, using e.preventDefault() is preferred over returning false; 同样,优先使用e.preventDefault()而不是返回false。

$(document).ready(function () {
    $("button").click(function (e) {
        e.preventDefault();
        $("#divTest1").hide();
        $("p").hide();
    });
});

Add a return false to the click event of the button, otherwise it will submit the form and refresh the page since you didn't define the button type and it is the only button. 在按钮的click事件中添加return false,否则将提交表单并刷新页面,因为您没有定义按钮类型,并且它是唯一的按钮。

$(document).ready(function () {
    $("button").click(function () {
        $("#divTest1").hide();
        $("p").hide();
        return false;
    });
});

http://jsfiddle.net/wefKu/ http://jsfiddle.net/wefKu/

You jQuery is not defined, you have added the Visual Studio jQuery documentation reference and not the actual jQuery library. 您的jQuery未定义,您添加了Visual Studio jQuery文档参考,而不是实际的jQuery库。

Have you tried adding the actual jQuery reference? 您是否尝试过添加实际的jQuery参考?

<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>

or 要么

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

and not 并不是

<script src="Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
$(document).ready(function() {
    $("button").click(function() {
        $("#divTest1").hide();
        $("p").hide();

        return false; // after you click, stop the default form action to avoid the redirect
    });
});​

you can try 你可以试试

$(document).ready(function () {
$("button").click(function (event) {
    event.preventDefault();
    $("#divTest1").hide();
    $("p").hide();
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM