Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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/0/asp.net-mvc/16.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
获取jQuery切换函数的动态id_Jquery_Asp.net Mvc - Fatal编程技术网

获取jQuery切换函数的动态id

获取jQuery切换函数的动态id,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我有这个Jquery函数 <script> $(function () { $("tr").next().find("div").hide(); $("td[colspan=3]").find("p").hide(); $("#status_0").click(function (event) { event.stopPropagation(); var $target = $(event.target);

我有这个Jquery函数

<script>
$(function () {
    $("tr").next().find("div").hide();
    $("td[colspan=3]").find("p").hide();        
    $("#status_0").click(function (event) {
        event.stopPropagation();
        var $target = $(event.target);
        if ($target.closest("td").attr("colspan") > 1) {
            $target.slideUp();
        } else {
            $target.closest("tr").next().find("div").slideToggle();
        }
    });
});

$(函数(){
$(“tr”).next().find(“div”).hide();
$(“td[colspan=3]”。查找(“p”).hide();
$(“状态0”)。单击(函数(事件){
event.stopPropagation();
var$target=$(event.target);
如果($target.closest(“td”).attr(“colspan”)>1){
$target.slideUp();
}否则{
$target.closest(“tr”).next().find(“div”).slideToggle();
}
});
});

我用这种方式生成了动态id:

<a id="status_@commandLine.lineRowId"><img src="~/fonts/details.png" style="width:40px; height:32px;" /></a>

我试图将onclick事件添加到我的锚中,但我的行为不稳定。。。
稍微精确一点,我在一个表的td上生成我的id,jQuery允许您使用类似通配符的选择器来定位元素。如果您知道所有
元素都将有一个动态生成的id,其语法为
#status
,请使用以下选择器绑定单击事件

var consistent_dynamic_string = 'status_';
$('a[id*="' + consistent_dynamic_string + '"]').each(function(anchor_index, anchor_element) {
    $(anchor_element).click(function(event) {
        event.stopPropagation();
        var $target = $(event.target);
        if ($target.closest("td").attr("colspan") > 1) {
            $target.slideUp();
        } else {
            $target.closest("tr").next().find("div").slideToggle();
        }
    });
});

在所有元素上使用公共类,或者基于元素的父元素使用选择器,例如
#container>div。a
你能定义什么是不稳定行为吗?你应该提供所有相关的HTML标记,包括你提到的表格。@MasterYoda我的意思是,当我点击例如我的第一个按钮(由动态id生成)时,它是切换2 3次箭头,当我点击另一个按钮时,它切换2 3个div(与其他按钮相关)…@LaalaDjelouah我的代码中有一个输入错误。您的选择器应该是
$('a[id*=“'+一致的\u动态\u字符串+”)