简体   繁体   English

使用JavaScript从HTML中的输入标签获取类型

[英]get the type from input tag in HTML using JavaScript

The application creates a temp HTML Page for Print Version. 该应用程序为打印版本创建一个临时HTML页面。 I can disable everything on the page so that user can not interact with the page..but that makes evrything grey in color and user is having problem with reading. 我可以禁用页面上的所有内容,以便用户无法与页面进行交互。但是,这会使颜色变灰,并且用户在阅读时遇到问题。 SO i want to make everything to be readonly.. 所以我想使所有内容都是只读的。

Here is my piece of code, 这是我的代码,

var x = 0;        
var element;
while (x < document.getElementsByTagName("input").length) {

  element =  document.getElementsByTagName("input")[x].type
  if (element = "BUTTON") {              
      document.getElementsByTagName("input")[x].onclick = null;
  }
  if (element = "TEXT") {
     document.getElementsByTagName("input")[x].readOnly = true;
  }
  if (element = "CHECKBOX") {
     document.getElementsByTagName("input")[x].disabled = true;
  }
  x++;     
}

if i remove the if block for checkbox it is working fine. 如果我删除复选框的if块,它工作正常。 but if i include the checkbox condition it is making everything disabled(even buttons and text)..when i debug i see all the if blocks are executing.. i do not understand why. 但是,如果我包括复选框条件,它将使所有功能都被禁用(即使按钮和文本也是如此)..当我调试时,我看到所有的if块都在执行。

Can some one plz help me out in this regard? 有人可以帮我这方面吗?

在检查相等性时,请立即使用===,这是在使用赋值运算符=,这意味着每次检查都将返回true。

比较使用==代替=

     if (element == "BUTTON") {  }               

如果条件使用==代替=内部。

If you simple want to disable everything on the page - a faster way to do it is place transparent DIV over page content with z-index of a higher value. 如果您只是想禁用页面上的所有内容 ,那么一种更快的方法是将透明DIV放置在页面内容中,将z-index值设为更高。

Something like this: 像这样:

Hello: <input type="text"/> <br>
Check this: <input type="checkbox" />

<div style="position: absolute; z-index: 10; top:0; left;0; width:100%; height:100%" />

Demo: http://jsfiddle.net/j39Au/ 演示: http//jsfiddle.net/j39Au/

I think the differential styling for disabled inputs is useful because it provides a visual feedback for the user. 我认为禁用输入的差异样式很有用,因为它为用户提供了视觉反馈。

This doesn't address your question directly but it may be a good alternative. 这不会直接解决您的问题,但可能是一个不错的选择。 You could apply styling to the disabled inputs to suit your preference, maybe something like this. 您可以将样式应用于禁用的输入以适合您的喜好,也许是这样的。

input[disabled=disabled] {
    background: rgba(255,0,0,0.05);
    border: dashed 1px rgba(255,0,0,0.1);    
    color: black;
    cursor: not-allowed;
}

Finally I made a workaround to this issue. 最后,我针对此问题做出了解决方法。

I converted all my elements(hyperlinks, dropdowns, text) to labels and displayed them in black color. 我将所有元素(超链接,下拉列表,文本)转换为标签,并以黑色显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM