Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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
Javascript 更改属性未按预期工作_Javascript_Jquery - Fatal编程技术网

Javascript 更改属性未按预期工作

Javascript 更改属性未按预期工作,javascript,jquery,Javascript,Jquery,我用下面的例子来描述我的问题: <table> <tr> <td> <button id="arm" disabled>Arm</button> </td> <td> <button id

我用下面的例子来描述我的问题:

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(“tr>td>#arm”)。在(“单击”,函数(){
$(this.parent().children(#arm”).prop(“disabled”,true);
$(this.parent().children(#disarm”).prop(“disabled”,false);
});
$(“tr>td>#解除武装”)。打开(“单击”,函数(){
$(this.parent().children(#arm”).prop(“disabled”,false);
$(this.parent().children(#disarm”).prop(“disabled”,true);;
});

手臂
解除武装

尝试从元素中完全删除禁用属性。据我所知,并非所有浏览器都能正确使用disable=“false”

尝试从元素中完全删除disable属性。据我所知,并非所有浏览器都能正确使用disable=“false”

您选择了正确的元素。你的代码是

$(this).parent().children("#disarm")
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
其中
$(this)
是#arm元素,然后
parent()
是td单元

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
所以基本上你是在搜索那个牢房的一个儿童元素(#disarm),这就是为什么他找不到它

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
试试这个

$("tr > td > #arm").on("click", function() {
    $(this).prop("disabled", true);
    $(this).closest("tr").find("#disarm").prop("disabled", false);
});

$("tr > td > #disarm").on("click", function() {
    $(this).closest("tr").find("#arm").prop("disabled", false);
    $(this).prop("disabled", true);
}); 
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

这是您的代码的一部分:)

您没有选择正确的元素。你的代码是

$(this).parent().children("#disarm")
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
其中
$(this)
是#arm元素,然后
parent()
是td单元

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
所以基本上你是在搜索那个牢房的一个儿童元素(#disarm),这就是为什么他找不到它

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
试试这个

$("tr > td > #arm").on("click", function() {
    $(this).prop("disabled", true);
    $(this).closest("tr").find("#disarm").prop("disabled", false);
});

$("tr > td > #disarm").on("click", function() {
    $(this).closest("tr").find("#arm").prop("disabled", false);
    $(this).prop("disabled", true);
}); 
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

这是一段代码:)

您可以添加一个类,然后使用下面的prop函数

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <button id="arm" disabled class="test">Arm</button>
                </td>
                <td>
                    <button id="disarm" class="test">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
        $('.test').click(function(){
            $(".test").prop('disabled', function () {
   return ! $(this).prop('disabled');
});
});
        </script>
    </body>
</html>
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

手臂
解除武装
$('.test')。单击(函数(){
$(“.test”).prop('disabled',function(){
return!$(this.prop('disabled');
});
});

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

说明:我为所有按钮添加了一个类测试,然后使用了prop功能。因此,单击事件发生的按钮被禁用,rest all被启用

您可以添加一个类,然后使用下面的prop函数

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <button id="arm" disabled class="test">Arm</button>
                </td>
                <td>
                    <button id="disarm" class="test">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
        $('.test').click(function(){
            $(".test").prop('disabled', function () {
   return ! $(this).prop('disabled');
});
});
        </script>
    </body>
</html>
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

手臂
解除武装
$('.test')。单击(函数(){
$(“.test”).prop('disabled',function(){
return!$(this.prop('disabled');
});
});

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

说明:我为所有按钮添加了一个类测试,然后使用了prop功能。因此,单击事件发生的按钮被禁用,rest all被启用

您需要使用
prev
next

$("tr > td > #arm").on("click", function() {
    $(this).parent().children("#arm").prop("disabled", true);
    $(this).parent().next().children("#disarm").prop("disabled", false);
});
$("tr > td > #disarm").on("click", function() {
    $(this).parent().prev().children("#arm").prop("disabled", false);
    $(this).parent().children("#disarm").prop("disabled", true);;
});
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

您需要使用
prev
next

$("tr > td > #arm").on("click", function() {
    $(this).parent().children("#arm").prop("disabled", true);
    $(this).parent().next().children("#disarm").prop("disabled", false);
});
$("tr > td > #disarm").on("click", function() {
    $(this).parent().prev().children("#arm").prop("disabled", false);
    $(this).parent().children("#disarm").prop("disabled", true);;
});
           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

这是您的问题的解决方案。使用removeAttr删除元素的禁用项

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

手臂
解除武装
$(“tr>td>#arm”)。在(“单击”,函数()上{
$(“手臂”).prop(“残疾”,真);
$(“解除武装”).removeAttr(“禁用”);
});
$(“tr>td>#解除武装”)。打开(“单击”,函数(){
$(“#手臂”)。移除字幕(“禁用”);
$(“#解除武装”).prop(“禁用”,正确);;
}); 

以下是一个工作示例的示例

以下是您问题的解决方案。使用removeAttr删除元素的禁用项

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>

手臂
解除武装
$(“tr>td>#arm”)。在(“单击”,函数()上{
$(“手臂”).prop(“残疾”,真);
$(“解除武装”).removeAttr(“禁用”);
});
$(“tr>td>#解除武装”)。打开(“单击”,函数(){
$(“#手臂”)。移除字幕(“禁用”);
$(“#解除武装”).prop(“禁用”,正确);;
}); 

下面是一个工作示例的示例

单击按钮时,没有理由选择父项

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(this).parent().children("#arm").prop("disabled", true);
应该是

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(this).prop("disabled", false);
现在,当您尝试选择另一个元素时,您可以这样做

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
  • 您可以选择按钮的父级
  • 您可以在该元素中查找另一个按钮
问题是按钮位于父级的同级中。但是,当您有id时,不需要选择父项。只需按id选择它即可启动,这样会更快

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(“表tbody”)。在(“单击”,“按钮”,函数()上){
var btn=$(this).prop(“已禁用”,true);
var CONVERTIVE=btn.attr(“id”)=“解除武装”?“解除武装”:“解除武装”;
$(相反).prop(“已禁用”,false);
});

手臂
解除武装

单击按钮时,没有理由选择父项

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(this).parent().children("#arm").prop("disabled", true);
应该是

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(this).prop("disabled", false);
现在,当您尝试选择另一个元素时,您可以这样做

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
  • 您可以选择按钮的父级
  • 您可以在该元素中查找另一个按钮
问题是按钮位于父级的同级中。但是,当您有id时,不需要选择父项。只需按id选择它即可启动,这样会更快

           <table>
            <tr>
                <td>
                    <button id="arm" disabled>Arm</button>
                </td>
                <td>
                    <button id="disarm">Disarm</button>
                </td>
            </tr>
        </table>

        <script>
            $("tr > td > #arm").on("click", function() {
                $("#arm").prop("disabled", true);
                $("#disarm").removeAttr("disabled");
            });

            $("tr > td > #disarm").on("click", function() {
               $("#arm").removeAttr("disabled");
               $("#disarm").prop("disabled", true);;
            }); 
        </script>
$(“表tbody”)。在(“单击”,“按钮”,函数()上){
var btn=$(this).prop(“已禁用”,true);
var CONVERTIVE=btn.attr(“id”)=“解除武装”?“解除武装”:“解除武装”;
$(相反).prop(“已禁用”,false);
});

手臂
解除武装

id
在整个页面上必须是唯一的。如果实际代码中有多行,这将是一个问题。使用
或<