登记册-Click事件与jQuery - 不点火 [英] Register On-Click Event with jQuery - Not Firing

查看:107
本文介绍了登记册-Click事件与jQuery - 不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个匆匆...

Should be a quickie ...

1)我使用MVC创建了一个新的项目在VS2013。结果
2)添加以下code到与该项目模板创建的默认主页\\ Index.cshtml的底部。

1) I created a new project in VS2013 using MVC.
2) I added the following code to the bottom of the default Home\Index.cshtml that is created with the project template.

<button class="myalertclass">Alert me</button>

<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")">
    $(document).on('click', '.myalertclass', function (e) {
        alert('Hello you');
    })
</script>

我现在期待,当我点击提醒我按钮,我会用你好你的消息框,但没有这样的喜悦。

I was now expecting that when I click the "Alert me" button, I'll get a message box with "Hello you", but no such joy.

问题是,当我点击该按钮,该事件不会被解雇。我想我错过了一些布线的地方,但我难住了。

The problem is that when I click the button, the event is not fired. I guess I'm missing some wiring somewhere, but am stumped.

推荐答案

据的 w3.org (重点煤矿):

脚本可以在 SCRIPT 元素的内容中或在外部文件中定义。如果未设置的src 属性,用户代理必须跨preT元素为脚本的内容。 如果在的src 有一个URI值,用户代理必须忽略元素的内容,并通过URI检索脚本。

The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

这就是说,你不能同时指定的src 的为你的身体脚本元素,并希望双方的工作......所以,使用两个单独的剧本元素。

That's to say, you can't specify both a src and a body for your script element and expect both to work... so, use two separate script elements.

<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script>
    $(document).on('click', '.myalertclass', function (e) {
        alert('Hello you');
    })
</script>

这篇关于登记册-Click事件与jQuery - 不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆