Jquery属性代码为Javascript属性代码

时间:2015-01-22 01:38:20

标签: javascript jquery attributes

我的项目中有一个场景,我不应该使用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代码相同并使其正常工作的帮助。

Jquery代码

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;
    });
}

1 个答案:

答案 0 :(得分:0)

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

Codepen example



var inputs = document.querySelectorAll("input");

for(i = 0; i < inputs.length; i++){
  inputs[i].disabled = true;
}
&#13;
&#13;
&#13;