0

我需要我的 asp.net mvc3 应用程序中的点击跟踪模块。问题是如何在 View (Razor) ASP.NET MVC3 中的单个事件中管理所有元素(文本框、按钮、超链接标记、段落标记、div 标记...)。我可以将委托事件方法()与 jquery 一起使用来管理所有事件吗???

下面是我的“查看页面”Html 代码:

<p>Click this paragraph1.</p>
<p>Click this paragraph2.</p>
<p>Click this paragraph3.</p>
<p>Click this paragraph4.</p>
<div style="background-color:yellow">
heyyy
<button>Click me!</button>
<input type="text">
<a href="http://stackoverflow.com/users/1841626/user1841626">user1841626</a>
</div>

那是我的 jquery 脚本:

<script src="jquery.js"></script>
<script>
$(document).ready(function(){
  $("div").delegate("button","click",function(){
    alert("The button was clicked.");
  })
 $("p").on("click",function(){
  alert("The paragraph was clicked.");
 })
 $("input ").on("click",function(){
  alert("The textBox was clicked.");
 })
 $("a ").on("click",function(){
  alert("The user1841626 was clicked.");
 })
});
</script>
4

1 回答 1

0

如果我理解正确你想要

$(document).ready(function(){
    $('div').on('click','button, p, input, a',function(e){
        var tag = this.tagName.toLowerCase(),
            message = '';
        switch(tag){
            case 'a': 
                message = 'user1841626';
                break;
            case 'button': 
                message = 'button';
                break;
            case 'p': 
                message = 'paragraph';
                break;
            case 'input': 
                message = 'textBox';
                break;
        }
        alert('The ' + message + ' was clicked.');
    })
});
于 2012-11-23T16:27:41.040 回答