getelementbyid 相关问题

getElementById是浏览器中JavaScript常用的一种基本方法,用于通过ID检索HTML或XML文档中的特定元素节点。

我可以使用带有多个 id 的 document.getElementById() 吗?

doStuff(document.getElementById("myCircle1" "myCircle2" "myCircle3" "myCircle4")); 这不起作用,所以我需要逗号或分号才能使其起作用吗?

回答 18 投票 0

按钮 ID 作为 getElementById

我可以在“getElementById”中获取触发该功能的按钮ID值吗? 我正在尝试编写一个通用函数,其中 getElementById 应该与按钮 ID 相同,因此触发...

回答 4 投票 0

添加无限滚动回调函数

我有一个 tumblr,我在其中根据图像出现的帖子大小加载不同的 url,使用 javascript 和 tumblr 的主题运算符 {photoUrl-250}、{photoUrl-400} 等。用户 b ...

回答 1 投票 0

有什么理由使用 document.getElementById 而不是 ID 本身吗?

例如,要从 DOM 中删除元素,myEl.remove() 的效果与 document.getElementById("myEl").remove() 一样(在 Firefox 和 Chrome 中)

回答 2 投票 0

在 Python 中使用 Selenium 基于部分 ID 查找元素

我正在尝试使用硒从网站上获取一些天气数据。在 html 文件中,它们看起来像这样: 我正在尝试使用硒从网站上获取一些天气数据。在 html 文件中,它们看起来像这样: <div id="tdatmo1-FFrad6Noeud-20240225-0700" name="tdatmo1-FFrad6Noeud-20240225-0700" class="container_value " . ><div class='box-offset'>10</div></div> <div id="tdatmo1-FFrad6Noeud-20240225-0654" name="tdatmo1-FFrad6Noeud-20240225-0654" class="container_value " . ><div class='box-offset'>9</div></div> <div id="tdatmo1-FFrad6Noeud-20240225-0648" name="tdatmo1-FFrad6Noeud-20240225-0648" class="container_value " . ><div class='box-offset'>7</div></div> ID 的前半部分始终相同,但后半部分根据时间/日期而变化。 我想我可以使用driver.find_elements(By.ID, "tdatmo1-FFrad6Noeud"),但这似乎不起作用。我也知道 By.XPATH 和 PARTIAL_LINK_TEXT,但我不确定它们是否适用于我的情况。 我查看了解决方案here 并尝试了driver.find_element(By.XPATH, "//*[contains(@id, 'tdatmo1-FFrad6Noeud')]"),这给了我<selenium.webdriver.remote.webelement.WebElement (session="007ad7b2-c5ba-46a8-8f5b-cbe0ea396b55", element="1c616c23-d457-4799-9c20-5a831d2895f1")> 我不确定这是否给了我想要的东西,而且它绝对不是我能理解的形式。 你可以点击它给你的没有headless的元素,然后看看它是否是。 webdriver.click()

回答 1 投票 0

Ajax 响应作为 DOM 对象

有没有办法从典型的ajax函数中获取响应,以便可以用getElements对其进行剖析?我尝试过 query.responseText.getElementById 但它的效果和看起来一样糟糕。你...

回答 5 投票 0

未捕获类型错误:无法读取 null 的属性(读取“值”)getElementById

单击按钮时出现此错误。 未捕获的类型错误:无法读取 null 的属性(读取“值”) 我究竟做错了什么?这是代码: 单击按钮时出现此错误。 未捕获的类型错误:无法读取 null 的属性(读取“值”) 我做错了什么?这是代码: <table> <tr> <th>Height (inches)</th> <th>Width (inches)</th> <th>Length (inches)</th> <th>Pieces</th> <th>Bd Ft</th> <th>Lineal Ft</th> <th>Sqare Ft</th> </tr> <tr> <td> <input type="text" value="1" id="height" /></td> <td> <input type="text" value="1" id="width" /></td> <td> <input type="text" value="1" id="length" /></td> <td> <input type="text" value="1" id="pieces" /></td> <td> <input type="text" value="" id="bdft" /></td> <td> <input type="text" value="" id="linealft" /></td> <td> <input type="text" value="" id="sqft" /></td> </tr> </table> <input type="button" value="Calculate" onclick="calculate()" /> <script type="text/javascript"> function calculate() { var heightVar = document.getElementById(height); var widthVar = document.getElementById(width); var lengthVar = document.getElementById(length); var piecesVar = document.getElementById(pieces); alert(document.getElementById(height).value); } </script> 我做错了什么?我希望在警报窗口中获得“高度”值。 我缺少引文(废话)。 var heightVar = document.getElementById(height); 需要 var heightVar = document.getElementById("高度");

回答 1 投票 0

js for 循环innerHTML 函数在加载相同id 时不起作用

for循环innerHTML +=不起作用 富巴 document.getElementById('outSecs').</desc> <question vote="-1"> <p>在 addeventlistener 负载上调用函数时,for 循环 insideHTML += 不起作用</p> <pre><code>&lt;main id=&#39;outSecs&#39;&gt; foobar &lt;/main&gt; &lt;script&gt; document.getElementById(&#39;outSecs&#39;).addEventListener(&#39;load&#39;, myFn); function myFn() { for (var q = 1; q &lt;= 9; q++) { document.getElementById(&#39;outSecs&#39;).innerHTML += &#34;&lt;section&gt;&lt;p class=&#39;myOutput&#39;&gt;&lt;/p&gt;&lt;/section&gt;&#34;; } } </code></pre> <p>如何在加载时插入带有innerHTML的循环代码?</p> </question> <answer tick="false" vote="0"> <p>我建议你使用窗口的 DOMContentLoaded 事件。</p> <p>代码也可以更优雅。无需每次都获取主容器。只需获取一次并填充即可。</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>const myFn = () =&gt; { const mainContainer = document.getElementById(&#39;outSecs&#39;); mainContainer.innerHTML = Array.from({ length: 9 }) .map((_, i) =&gt; `&lt;section id=&#34;s${i}&#34;&gt;&lt;p class=&#34;myOutput&#34;&gt;${i+1}&lt;/p&gt;&lt;/section&gt;`) .join(&#39;&#39;) }; window.addEventListener(&#39;DOMContentLoaded&#39;, myFn)</code></pre> <pre><code>&lt;main id=&#39;outSecs&#39;&gt; foobar &lt;/main&gt;</code></pre> </div> </div> <p></p> </answer> </body></html>

回答 0 投票 0

让接收0但不接收无效值

我有一个输入,我想接收任何数字(不管它是否为负数、浮点数、长整型),我将其编码为现在允许 null、未定义或空值,但如果我输入 0,它将被读取为空 常量

回答 1 投票 0

为什么 getElementByID 返回 Nothing 而不是 NULL

在 VBA 中,如果我使用 getElementByID("id_name") 并且 id 不存在,则该函数不会返回任何内容,而不是 null。这让我不知道 DOM 是否尚未渲染该元素,或者 el...

回答 2 投票 0

javascript 循环和使用参数设置 documentGetElementById 的问题

我正在研究星级评级系统。我想在星星图像列表上设置事件侦听器,以便单击星星可以用实心或空白星星填充星星。问题是当

回答 1 投票 0

python-Django,如何将项目ID传递给特定用户?

大家 我创建了一个项目模型,使管理员能够创建单独的项目并管理它们,其中每个项目都有自己的流程,因为它们的结构相似且不同......

回答 1 投票 0

javascript 使用一个 getElementById 多次随机数

我想在跨度 ID 中多次使用随机数结果 但即使克隆或委托结果也只会在结果中出现一次,而其他结果则显示为空白 我不知道该怎么做,他可以吗...

回答 1 投票 0

HTML 上的 document.getElementById 出现 Null 错误

函数 myFunction() { 变量名称=“”; var 密码 = ""; name = document.getElementById('name');...</desc> <question vote="-1"> <pre><code>&lt;body&gt; &lt;script lang=&#34;javascript&#34;&gt; function myFunction() { var name = &#34;&#34;; var password = &#34;&#34;; name = document.getElementById(&#39;name&#39;); password = document.getElementById(&#39;password&#39;); alert(name) alert(password) } &lt;/script&gt; &lt;h1 id=&#34;Center&#34;&gt;Log in&lt;/h1&gt; &lt;input type=&#34;text&#34; id=&#34;Center&#34; id=&#34;name&#34; style=&#34;background-color: rgb(255, 255, 255); border-radius: 0px;&#34;&gt; &lt;input type=&#34;text&#34; id=&#34;Center&#34; id=&#34;password&#34; style=&#34;background-color: rgb(255, 255, 255); border-radius: 0px;&#34;&gt; &lt;input type=&#34;button&#34; id=&#34;Center&#34; style=&#34;width: 170px;&#34; onclick=&#34;myFunction()&#34; value=&#34;Submit&#34;&gt; &lt;/body&gt; </code></pre> <p><pre><code>I want to make the input text to alert and I get data of text using a document.getElementById and there has a two text and one button and id center is the code that the object can go to the center place on the screen.</code></pre></p> <p>我试着把它串起来或者再写一次,但没有办法做得好。</p> </question> <answer tick="false" vote="0"> <p>尝试这样:</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; #Center { display: block; margin: auto; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;script lang=&#34;javascript&#34;&gt; function myFunction() { var name = &#34;&#34;; var password = &#34;&#34;; name = document.getElementById(&#39;name&#39;).value; password = document.getElementById(&#39;password&#39;).value; alert(&#34;Name: &#34; + name + &#34;\nPassword: &#34; + password); } &lt;/script&gt; &lt;h1 id=&#34;Center&#34;&gt;Log in&lt;/h1&gt; &lt;input type=&#34;text&#34; id=&#34;name&#34; style=&#34;background-color: rgb(255, 255, 255); border-radius: 0px;&#34; placeholder=&#34;Enter your name&#34;&gt; &lt;input type=&#34;password&#34; id=&#34;password&#34; style=&#34;background-color: rgb(255, 255, 255); border-radius: 0px;&#34; placeholder=&#34;Enter your password&#34;&gt; &lt;input type=&#34;button&#34; id=&#34;Center&#34; style=&#34;width: 170px;&#34; onclick=&#34;myFunction()&#34; value=&#34;Submit&#34;&gt; &lt;/body&gt; &lt;/html&gt;</code></pre> </div> </div> <p></p> </answer> </body></html>

回答 0 投票 0

javascript getelement,其中值包含特定字符串

我想计算具有文本类名称的输入字段的出现次数,其中该文本输入包含特定值。 document.getElementById('c_files').getElementsByClassName('tex...

回答 5 投票 0

如何使用Javascript获取id?

如果我知道的是这个文本:8039089332,我该如何提取文本:mytr161 如果我所知道的文本是:8039089332,我该如何提取文本:mytr161 <tr bgcolor="#dcdcdc" id="mytr161"> <td align="right" bgcolor="#BDB9B4"><input type="checkbox" id="ck161" name="ck161" value="1" onclick="javascript:changebgcolor('161');"></td> <td align="center" nowrap=""><small><font size="2"> <a href="MfSchOpen850PODetail.asp?lpo=8039089332&amp;litem=hidingit&amp;Plant=hidingitaswell&amp;lsimplifyordertype=hidden" target="Podetail">8039089332</a> </font></small>&nbsp;</td> 我有一个数字列表,如文本:8039089332。我想单击它旁边的复选标记。为此,我正在考虑如何编写一段javascript代码来循环遍历数字列表,然后单击复选标记,我认为,首先需要找到文本的id:8039089332。然后可能找到一种方法来修剪id删除文本“mytr”并将其替换为“ck”。 这是我第一次尝试 javascript,我现在不知道该怎么做,因为我无法提取我拥有的数字列表的 id。 //获取距离按钮最近的元素 varclosestTR=button.closest('tr'); //获取最近的id属性 var trId = 最接近的TR.id; // 用提取的 id 做一些事情(例如,将其记录到控制台) console.log('TR ID:', trId);

回答 1 投票 0

我必须多次使用我的脚本与 document.getElementById("")

我正在制作一个布局,其中每个框都必须使用... id =“top1”,id =“top2”,id =“top3”等...这样当您单击它时,它就会转到文档顶部,但我找不到...

回答 2 投票 0

getElementById 在 Javascript 页面构建中返回 null

我用javascript创建了结构。我不使用html。 我有一个创建结构并返回根窗格的函数。 函数 createEmptyChartPane (chartId) { 让 divlem = 文档。

回答 1 投票 0

获取 if/else 语句的元素 ID

正在做一个项目。我需要 else 语句才能工作,但是当我点击提交按钮时,它只是重定向到“if” var 判断量 = document.getElementById('</desc> <question vote="0"> <p>正在从事一个项目。我需要 else 语句才能工作,但是当我点击提交按钮时,它只是重定向到“if”</p> <pre><code>&lt;head&gt; &lt;script&gt; var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;); var amountCollected = document.getElementById(&#39;amountCollected&#39;); function judgmentCalc() { if( judgmentAmount - amountCollected &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); }else { window.open( &#39;http://www.yahoo.com&#39; ); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Application&lt;/h1&gt; &lt;p&gt;I understand that this application neither obligates me to &lt;b&gt;Affirmative Judgment&lt;/b&gt; nor does it obligate &lt;b&gt;Affirmative Judgment&lt;/b&gt; to me.&lt;/p&gt; &lt;h3&gt;Judgment Information&lt;/h3&gt; &lt;form&gt; &lt;span&gt;Do you have a case number?&lt;/span&gt; &lt;input type=&#34;text&#34; name=&#34;caseNumber&#34; /&gt; &lt;span&gt;Amount of Judgment&lt;/span&gt; &lt;input type=&#34;text&#34; name=&#34;judgmentAmount&#34; id=&#34;judgmentAmount&#34; /&gt; &lt;span&gt;Amount collected to date&lt;/span&gt; &lt;input type=&#34;text&#34; name=&#34;amountCollected&#34; id=&#34;amountCollected&#34; /&gt; &lt;input type=&#34;submit&#34; name=&#34;submitButton&#34; onclick=&#34;judgmentCalc();&#34; /&gt; &lt;/form&gt; </code></pre> </question> <answer tick="false" vote="0"> <p>您需要访问这些输入字段的值,而不是直接处理 <pre><code>document.getElementById()</code></pre> 返回的 dom 元素。您可以使用 <pre><code>input</code></pre> 属性获取 <pre><code>.value</code></pre> 的值。</p> <pre><code>var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;).value; var amountCollected = document.getElementById(&#39;amountCollected&#39;).value; </code></pre> </answer> <answer tick="false" vote="0"> <p>将变量移到函数内部,以便在函数执行时正确填充它们,并添加 DOM 元素的 .value 属性。</p> <p>试试这个:</p> <pre><code> function judgmentCalc() { var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;).value; var amountCollected = document.getElementById(&#39;amountCollected&#39;).value; if( judgmentAmount - amountCollected &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); }else { window.open( &#39;http://www.yahoo.com&#39; ); } } </code></pre> </answer> <answer tick="false" vote="0"> <p>您只是比较对象,您需要比较它们的值</p> <pre><code>var judgmentAmount = Number(document.getElementById(&#39;judgmentAmount&#39;).value); var amountCollected = Number(document.getElementById(&#39;amountCollected&#39;).value); </code></pre> <p>另外,在比较之前将它们转换为数字。</p> <pre><code>function judgmentCalc() { var judgmentAmount = Number(document.getElementById(&#39;judgmentAmount&#39;).value); var amountCollected = Number(document.getElementById(&#39;amountCollected&#39;).value); if( judgmentAmount - amountCollected &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); }else { window.open( &#39;http://www.yahoo.com&#39; ); } } </code></pre> </answer> <answer tick="false" vote="0"> <p>您将在此处获取 HTML DOM 元素:</p> <pre><code>var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;); var amountCollected = document.getElementById(&#39;amountCollected&#39;); </code></pre> <p>因此,在您的 <pre><code>judgementCalc</code></pre> 方法中,您需要获取这些元素的值:</p> <pre><code>function judgmentCalc() { // notice that we&#39;re using VALUE here: if( judgmentAmount.value - amountCollected.value &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); }else { window.open( &#39;http://www.yahoo.com&#39; ); } } </code></pre> </answer> <answer tick="false" vote="0"> <p>您需要根据元素的值进行操作:</p> <pre><code>function judgmentCalc() { var judgmentAmount = +document.getElementById(&#39;judgmentAmount&#39;).value; var amountCollected = +document.getElementById(&#39;amountCollected&#39;).value; if( judgmentAmount - amountCollected &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); } else { window.open( &#39;http://www.yahoo.com&#39; ); } } </code></pre> <p>var <pre><code>judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;)</code></pre> 仅返回对 ID 为“judgmentAmount”的元素的引用。如果你想使用元素的值,你需要这样做:</p> <p><pre><code>var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;).value</code></pre>。 </p> <p>下一步是计算一些金额。您需要将 string 转换为 int (使用 <pre><code>+</code></pre> 运算符)</p> <pre><code>typeof judgmentAmount; // &#34;string&#34; typeof +judgmentAmount; // &#34;number&#34; </code></pre> <p><a href="https://jsfiddle.net/ova74kmh/" rel="nofollow">演示</a></p> </answer> <answer tick="false" vote="-1"> <p>请运行下面的脚本,它会正常工作:</p> <pre><code>&lt;head&gt; &lt;script&gt; function judgmentCalc() { var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;).value; var amountCollected = document.getElementById(&#39;amountCollected&#39;).value; var exp = parseFloat(judgmentAmount) - parseFloat(amountCollected); if( exp &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); } else { window.open( &#39;http://www.yahoo.com&#39; ); } } &lt;/script&gt; &lt;/head&gt; </code></pre> </answer> <answer tick="false" vote="-1"> <p>您必须比较输入元素的值,请尝试以下操作</p> <pre><code>function judgmentCalc() { var judgmentAmount = document.getElementById(&#39;judgmentAmount&#39;).value; var amountCollected = document.getElementById(&#39;amountCollected&#39;).value; if( (judgmentAmount - amountCollected) &lt; 10000 ) { window.open( &#39;http://afjudgment.com&#39; ); } else { window.open( &#39;http://www.yahoo.com&#39; ); } } </code></pre> <p>它可能对你有帮助。</p> </answer> </body></html>

回答 0 投票 0

Blazor JSInterop 从 DOM 获取元素属性

我使用 Visual Studio 2022 样板项目制作了 Blazor 服务器端 Web 应用程序。我仍在使用 .NET 7,因为我的公司更新速度很慢。 我已经按照本教程使用 JSIn...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.