Jquery 属性代码到 Javascript 属性代码

Jquery attribute code to Javascript attribute code

本文关键字:属性 代码 Javascript Jquery      更新时间:2023-09-26

我的项目中有一个场景,我不应该使用Jquery代码(不幸的是)。我不擅长编写jquery代码,但不擅长编写javascript代码。

功能性:

1.I have few disabled controls on page with attribute clickdisabled=disable.    
2. I am trying to set title to all elements with that attribute on a page(s). 
3. when user clicks on disabled controls alert that controls title.
4. I am trying to make click on disabled controls

任何帮助,使我的java脚本代码与jquery代码相同并使其工作。

J查询代码

function DisableControlAction() {
   $('[clickdisabled=disable]').attr("title", "You are not authorized to perform this action.");
    $('[clickdisabled=disable]').removeAttr("disabled"); // to enable click for server controls
    $('[clickdisabled=disable]').click(function (e) {
        e.preventDefault();
        alert($(this).attr("title"));
        return false;
    });
}

我正在尝试的 Java 脚本代码

function DisableControlAction() {
document.getElementsByTagName('[clickdisabled=disable]').setAttribute("title", "You are not authorized to perform this action.");
document.getElementsByTagName('[clickdisabled=disable]').removeAttribute("disabled");
document.getElementsByTagName('[clickdisabled=disable]').click(function (e) {
        e.preventDefault();
        alert($(this).getAttribute("title"));
        return false;
    });
}

如果您使用 querySelectorAll() 并通过任何方式获取元素列表。您可以使用 element.disabled = true;它将禁用输入

代码笔示例

var inputs = document.querySelectorAll("input");
for(i = 0; i < inputs.length; i++){
  inputs[i].disabled = true;
}