【问题标题】:How to access certain element of a static webpage(html not accesible) through javascript?如何通过javascript访问静态网页的某些元素(html不可访问)?
【发布时间】:2018-09-30 11:34:02
【问题描述】:

在这里,我想在单击类“btn”的元素时调用某个函数……并按空格(键码 = 32)……但它不起作用……我做错了什么? 注意:我无法访问 html 代码...试图通过使用脚本使其工作

<body>
      <div class="container">
          <div class="row">
            <div class="autocreate-submit">
              <div class="panel-body">
               <br/>
                <form id="command" role="form" style="display:inline;" class="form-inline" action="Nextpage" method="post" enctype="multipart/form-data"> 
                  <div class="form-group">
                    <input type="submit" tabindex="2" class="btn" value="Next" />                       
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>


    <script>
    (function() {
        'use strict';
        document.addEventListener('mouseup', function(){
            alert("Mouse clicked")});
        document.addEventListener('keypress', function(x){
           if(x.keycode === 32){
           alert("Space pressed")};
        });
    })();
    </script>
    </body>

【问题讨论】:

  • 请交叉检查您的脚本是否被注入到与您打算的 html 不同的 dom 中,使用 iframe 等情况可能是您面临的问题。
  • 你能帮我解决这个问题吗...

标签: javascript dom element addeventlistener


【解决方案1】:

Dhaval,您可以使用类名访问 html 文档中的某些元素,例如使用下面的代码。

document.getElementsByClassName("btn")[0]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多