gpt4 book ai didi

javascript - 使用 jQuery 注册点击事件 - 不触发

转载 作者:行者123 更新时间:2023-11-29 19:45:59 25 4
gpt4 key购买 nike

应该是一个快速...

1) 我使用 MVC 在 VS2013 中创建了一个新项目。
2) 我将以下代码添加到使用项目模板创建的默认 Home\Index.cshtml 的底部。

<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>

我现在期望当我点击“提醒我”按钮时,我会收到一个带有“你好”的消息框,但没有这样的喜悦。

问题是当我点击按钮时,事件没有被触发。我想我在某处遗漏了一些接线,但我很困惑。

最佳答案

根据 w3.org (强调我的):

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.

也就是说,您不能同时为您的 script 元素指定 src 主体并期望两者都有效。 . 所以,使用两个单独的 script 元素。

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

关于javascript - 使用 jQuery 注册点击事件 - 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19689566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com