简体   繁体   English

使用 JavaScript 获取 div 中的元素

[英]Get Element in div with JavaScript

I want to disable/enable a button with JavaScript.我想用 JavaScript 禁用/启用按钮。 Because the JavaScript is called, after a Flash animation is rendered, the button exists at the time of the execution.因为调用了 JavaScript,所以在渲染一个 Flash 动画后,按钮在执行时就存在了。

The button is in a hierarchy:该按钮处于层次结构中:

<html><body><form#form1><div#control><asp:Button#Export1>

I tried for hours to get a reference to that button, but nothing seems to work:我尝试了几个小时来获取对该按钮的引用,但似乎没有任何效果:

document.getElementById("Export1")
// and
document.getElementbyId("form1").getElementById("control").getElementById("Export1")
// and many more

How to get a reference to that button (in order to change btnref.disabled = true )?如何获得对该按钮的引用(为了更改btnref.disabled = true )?

Thanks a lot for your help!非常感谢您的帮助!

Have you tried right-clicking in the document and selecting "view source" to see how that code actually renders?您是否尝试过在文档中右键单击并选择“查看源代码”以查看该代码的实际呈现方式? An asp:Button is a server control, that gets translated to an input field during render. asp:Button是一个服务器控件,它在渲染过程中被转换为输入字段。 During this, the ID of the field will not be exactly what you set it to in your aspx.在此期间,该字段的 ID 将与您在 aspx 中设置的不完全相同。

You can use Export1.ClientID at serverside to get the ID of the control.您可以在服务器端使用Export1.ClientID来获取控件的 ID。

如果它是您 div 中唯一的按钮,则应该可以:

var btnref = document.getElementById("controls").getElementsByTagName("button")[0];

Usually the id of the button won't stay the same in the page source.通常按钮的id在页面源中不会保持不变。 Click on view source in the HTML and look for that tag to find the new id.单击 HTML 中的view source并查找该标签以查找新 ID。 You can then use that id in something like:然后,您可以在以下内容中使用该 ID:

document.getElementbyId("Export1_some_unique_id")...

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

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