让html组件不可输入(只读),任何组件都有效

/**
 * 将页面设置为只读
 */
var setReadOnly = function()
{
    var input = $("input");
    input.each(function(i)
    {
        $(this).attr("onfocus", "this.blur();return false;");
        $(this).wrap(function()
        {
            return '<span onmousemove="this.setCapture();" onmouseout="this.releaseCapture();" />';
        });
    });
    
    var select = $("select");
    select.each(function(i)
    {
        $(this).attr("onfocus", "this.blur();return false;");
        $(this).wrap(function()
        {
            return '<span onmousemove="this.setCapture();" onmouseout="this.releaseCapture();" />';
        });
    });
    
    var textarea = $("textarea");
    textarea.each(function(i)
    {
        $(this).attr("onfocus", "this.blur();return false;");
        $(this).wrap(function()
        {
            return '<span onmousemove="this.setCapture();" onmouseout="this.releaseCapture();" />';
        });
    });
    
    var img = $("img");
    img.each(function(i)
    {
        $(this).attr("onfocus", "this.blur();return false;");
        $(this).wrap(function()
        {
            return '<span onmousemove="this.setCapture();" onmouseout="this.releaseCapture();" />';
        });
    });
}

以上代码用到JQuery。

原文地址:https://www.cnblogs.com/boazy/p/2992584.html