Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将单击事件绑定到具有特定类名的div中的所有链接_Asp.net Mvc_Jquery - Fatal编程技术网

Asp.net mvc 将单击事件绑定到具有特定类名的div中的所有链接

Asp.net mvc 将单击事件绑定到具有特定类名的div中的所有链接,asp.net-mvc,jquery,Asp.net Mvc,Jquery,document.ready中jquery调用的第一部分是伪代码。如何正确执行$('.mainLink').each().click(),以便NavigationPanel中所有类名为mainLinks的链接都绑定到click事件。不为链接使用id是否不好 $(document).ready(function () { $('.mainLink').each().click(function (e) { e.preventDefault();

document.ready中jquery调用的第一部分是伪代码。如何正确执行$('.mainLink').each().click(),以便NavigationPanel中所有类名为mainLinks的链接都绑定到click事件。不为链接使用id是否不好

$(document).ready(function () {

        $('.mainLink').each().click(function (e) {
                e.preventDefault();                
                $.ajax({
                    url: this.href,
                    beforeSend: OnBegin,
                    complete: OnComplete,
                    success: function (html) {
                        $('#ContentPanel').html(html);
                    }
                });
            });
        });  

<div id="NavigationPanel">  

        @Html.ActionLink("1", "Index", "First", null, new { @class = "mainLink" })
        @Html.ActionLink("2", "Index", "Two", null, new { @class = "mainLink"  })
        @Html.ActionLink("3", "Index", "Three", null, new { @class = "mainLink" })    
    </div>
$(文档).ready(函数(){
$('.mainLink')。每个()。单击(函数(e){
e、 预防默认值();
$.ajax({
url:this.href,
发送前:开始时,
完成:未完成,
成功:函数(html){
$('#ContentPanel').html(html);
}
});
});
});  
@ActionLink(“1”,“Index”,“First”,null,new{@class=“mainLink”})
@ActionLink(“2”,“Index”,“Two”,null,new{@class=“mainLink”})
@ActionLink(“3”,“Index”,“Three”,null,new{@class=“mainLink”})
只需执行
$('.mainLink')。单击(函数(e){
,该函数应将所有链接与类
绑定。mainLink

如果您想在div ID
NavigationPanel
中找到所有链接,请尝试以下操作

$('.mainLink',$('#导航面板'))。单击(函数(e){
只需执行
$('.mainLink')。单击(函数(e){
,它应将所有链接与类
绑定。mainLink

如果您想在div ID
NavigationPanel
中找到所有链接,请尝试以下操作


$('.mainLink',$('#导航面板'))。单击(函数(e){

如果您只想绑定到
#导航面板
中的所有
。mainLink
,以下工作:

$("#NavigationPanel").on("click", ".mainLink", function(e){
    e.preventDefault();                
    $.ajax({
        url: this.href,
        beforeSend: OnBegin,
        complete: OnComplete,
        success: function (html) {
            $('#ContentPanel').html(html);
        }
    });
});

如果您只想绑定到
#NavigationPanel
中的所有
.mainLink
,则以下操作有效:

$("#NavigationPanel").on("click", ".mainLink", function(e){
    e.preventDefault();                
    $.ajax({
        url: this.href,
        beforeSend: OnBegin,
        complete: OnComplete,
        success: function (html) {
            $('#ContentPanel').html(html);
        }
    });
});