标签: getelementbyid

document.getElementById(id) 并切换多个 id

我有这个简单的功能,可以切换网页中的隐藏元素。

function showtable(id) 
 {
 if(document.getElementById(id).style.display == 'block')
  {
document.getElementById(id).style.display = 'none';
  }else{
document.getElementById(id).style.display = 'block';
}
 } 


<input type="button" value="Toggle" onclick="showtable('id');" />
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想在每次单击按钮时关闭页面上的一些其他(表格)元素(具有某些 ID)(除了正在切换的元素,无论是打开还是关闭)。

javascript getelementbyid toggle

3
推荐指数
1
解决办法
1万
查看次数

如何在 JavaScript 中按类获取元素并将 aria-checked="false" 更改为 "true"?

我想在页面上选择多个类,并更改aria-checked="false"aria-checked="true"。这是我现在使用的脚本:

elms=document.getElementsByClassName("d-va-p a-f-e");
for (i=0;i<elms.length;i++){
    if(elms[i].getAttribute("aria-checked")="false")
        elms[i].getAttribute("aria-checked")="true"
};
Run Code Online (Sandbox Code Playgroud)

源代码如下。该课程是“d-va-p afe”。

<div class="d-va-p a-f-e" style="-webkit-user-select: none;" role="option" tabindex="-1" aria-checked="false" id=":89">
    <div class="a-f-e" style="-webkit-user-select: none;">
        <div class="d-va-p-Fe a-f-e" style="-webkit-user-select: none;">
            <img src="https://lh4.googleusercontent.com/-FxDBvNDkbj4/AAAAAAAAAAI/AAAAAAAAAAA/q4f7Fc7OF6A/s48-c/photo.jpg" class="d-va-p-Z a-f-e">
        </div>
        <div class="a-f-e d-va-p-yda" style="-webkit-user-select: none;">
            <div class="d-va-p-Ec a-f-e" style="-webkit-user-select: none;">Herb Smith</div>
        </div>
    </div>
    <div class="d-va-p-iI-oc" style="-webkit-user-select: none;">
        <span class="a-f-e d-J-Eb-v-YA-Me" style="-webkit-user-select: none;"></span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

javascript getelementbyid getattribute

3
推荐指数
1
解决办法
9367
查看次数

PHP DomDocument - getElementByID(部分匹配)如何?

有没有办法获取 id 部分匹配的所有元素。例如,如果我想获取网页上的所有 HTML 元素,其 id 属性以 开头,msg_但可以是此后的任何内容。

\n\n

这是我到目前为止所做的:

\n\n
$doc = new DomDocument;\n\n// We need to validate our document before refering to the id\n$doc->validateOnParse = true;\n$doc->loadHtml(file_get_contents(\'{URL IS HERE}\'));\nforeach($doc->getElementById(\'msg_\') as $element) { \n   foreach($element->getElementsByTagName(\'a\') as $link)\n   {\n      echo $link->nodeValue . "\\n";\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我需要弄清楚如何使用此位进行部分 id 匹配: $doc->getElementById(\'msg_\')或者是否有其他方法可以完成此操作...?

\n\n

基本上,我需要抓取所有 'a' 标签,这些标签是 id 开头的元素的子元素。从msg_ 技术上讲,总是只有 1 个a标签,但我不知道如何抓取第一个孩子,这就是为什么我也使用 foreach 的原因。

\n\n

DomDocument PHP 类可以实现这一点吗?

\n\n

这是我现在使用的代码,它也不起作用:

\n\n
$str = \'\';\n$filename = \'http://dream-portal.net/index.php/board,65.0.html\';\n@set_time_limit(0);\n\n$fp = fopen($filename, \'rb\');\nwhile …
Run Code Online (Sandbox Code Playgroud)

php getelementbyid domdocument

3
推荐指数
1
解决办法
1972
查看次数

JavaScript getElementById 返回 null

我正在学习 JavaScript,我想知道为什么会这样:

document.getElementById('partofid'+variable+number) 不起作用?

这是示例和JSfiddle链接,我希望“下一步”按钮删除显示的项目并显示下一个。

HTML:

<div id="div-1"> 1 </div>
<div id="div-2" style="display: none"> 2 </div>
<div id="div-3" style="display: none"> 3 </div>
<div id="div-4" style="display: none"> 4 </div>

<a id="next" href="#">next</a>
Run Code Online (Sandbox Code Playgroud)

JS:

var counter = 1;
var button = document.getElementById('next');

button.addEventListener("click",function(){
    var currentDiv = document.getElementById('div-'+counter);
    currentDiv.remove();
    var nextDiv = document.getElementById('div-'+counter+1);
    alert(nextDiv); // why does it return null
    alert('div-'+counter+1); // while this doesn't?
    nextQuestion.style.display = "block";
    counter++;
},true);
Run Code Online (Sandbox Code Playgroud)

javascript getelementbyid

3
推荐指数
1
解决办法
2134
查看次数

JavaScript:$('foo')vs document.getElementById('foo')

如果以前问过这个问题,请原谅我.

我是JavaScript的新手,我正在阅读以下博客文章:http: //www.dustindiaz.com/javascript-no-no/

他做的第一点是将document.getElementByID()调用移动到getter中.这对我来说很有意义,因为它使代码更加模块化和方便.然而,他接着说:

大多数人甚至更喜欢经典的Prototype $函数,它允许你传入任意数量的参数.这也很有效.

$('foo', 'bar', 'baz');
Run Code Online (Sandbox Code Playgroud)

我无法找到关于这种方法的文档,我是否正确地读到这相当于调用document.getElementById(),除了你可以在原型中有多个参数?

如果是这样,使用document.getElementbyId('foo')结束有什么好处$('foo')?

编辑:我刚刚意识到他在"原型"中大写了P.Prototype是某种外部框架吗?我感到印象深刻,它就像一条捷径或其他东西.

javascript getelementbyid

3
推荐指数
1
解决办法
5256
查看次数

WPF - 网络浏览器 - getElementById

在 WPF 应用程序中,我有一个名为 WebBrowser1 的网络浏览器。这是指包含用户可以输入文本的 TextArea 的 HTML 页面。

<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我希望获得此文本并可能还设置此文本。

我尝试了类似于 javascript 编写方式的方法:

document.getElementById("myStudentOutput1").innerHTML;
Run Code Online (Sandbox Code Playgroud)

HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];

dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

c# webbrowser-control getelementbyid

3
推荐指数
1
解决办法
7920
查看次数

如何循环浏览javascript中的按钮?

我的网站上有许多不同的按钮,我想为每个按钮分配不同的任务,目前我正在使用它们一个一个地引用它们getElementByID,因为我有太多的按钮,我最终编写了无数行代码来引用这些按钮,有一些 Java 知识,我知道我所做的事情可以用更少的代码使用 for 循环来实现,但我不太确定如何去做,欢迎任何帮助。

目前这是我引用我的按钮的方式:

    var Button1Menu = document.getElementById("button1menu"); 
    var Button2Menu = document.getElementById("button2menu"); 
    var Button3Menu = document.getElementById("button3menu"); 
    var Button4Menu = document.getElementById("button4menu"); 
    var Button5Menu = document.getElementById("button5menu"); 
    var Button6Menu = document.getElementById("button6menu"); 
    var Button7Menu = document.getElementById("button7menu"); 
    var Button8Menu = document.getElementById("button8menu"); 
    var Button9Menu = document.getElementById("button9menu"); 
    .....
    .....
    .....
    var Button43Menu = document.getElementById("button43menu"); 
Run Code Online (Sandbox Code Playgroud)

然后我为每个人分配点击侦听器:

Button1Menu.onclick = function() {

     //doing something

  };
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来实现这一点。

html javascript for-loop button getelementbyid

3
推荐指数
1
解决办法
1万
查看次数

尝试通过reactjs中的document.getgetElementsByTagName进行映射

当我运行时,我试图映射结果对象document.getElementsByTagName。我意识到我无法映射对象,只能映射数组,但我不知道如何将其转换为对象以使其可迭代。

我的代码如下所示:

const [h2, setH2] = useState([]);

const getH2 = () => {
setH2(document.getElementsByTagName("h2"));
console.log(h2)
  }
Run Code Online (Sandbox Code Playgroud)

这给了我以下结果: 控制台日志

尝试通过 h2 进行映射会返回错误:

TypeError: h2.map is not a function
Run Code Online (Sandbox Code Playgroud)

我也尝试过h2.HTMLCollection.map(),但它引发了同样的错误。

如何映射 的结果docuemnt.getElementsByTagName?我尝试将对象转换为数组,但到目前为止未成功。

javascript arrays object getelementbyid reactjs

3
推荐指数
1
解决办法
1416
查看次数

Javascript从打开的窗口中获取元素

我需要打开一个新窗口并返回其中包含的元素.

假设我们有页面A和页面B,我想要:

  • A打开B.
  • 得到对B感兴趣的元素
  • 将该元素返回给A.
  • 我试图以这种方式这样做,但它不起作用:

    var newwindow = window.open("http://www.example.com");
    var elem = newwindow.document.getElementById('my-id').value;
    
    Run Code Online (Sandbox Code Playgroud)

    我哪里错了?有人对我有什么建议吗?

    javascript browser getelementbyid

    2
    推荐指数
    1
    解决办法
    2783
    查看次数

    试图从tr标签获取类名 - 'undefined'

    我需要从给定的<tr>标签中获取类名,但我无法这样做.

    <table cellpadding=5 cellspacing=5>
        <tr id='cat_abc123' class='class_a'>
            <td>foo</td>
            <td><input type='checkbox' name='cb1' value='1' onClick="info(this, 'abc123')">
        </tr>
    </table>
    
     <script language='javascript'>
         function info(theElement, id)
         {
             tr_id = 'cat_' + id;
             alert(tr_id + ' ' + document.getElementById(tr_id).class);
         }
     </script>
    
    Run Code Online (Sandbox Code Playgroud)

    工作示例:http: //jsfiddle.net/rQpeu/

    我错过了什么?

    更新

    我使用了错误的描述符 - 应该使用Classname.感谢大家及时回复!更新了jsfiddle:http://jsfiddle.net/rQpeu/3/

    javascript class getelementbyid tablerow

    2
    推荐指数
    1
    解决办法
    8646
    查看次数