html JS如何访问id以下划线“_"开头的元素?[duplicate]

sulc1iza  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(141)

此问题在此处已有答案

Cross domain iframe issue(5个答案)
昨天关门了。
这篇文章是昨天编辑并提交审查的。
我想在我的网页中嵌入的YouTube订阅按钮上做一个类似于点击重定向的操作(用户点击span,另一个元素将通过编程得到.click())。在Google的脚本运行了初始标记之后,标记(简化)如下:

<!-- The property `otherproperties="notmentionedhere"` is a placeholder for the other properties those elements have, some of those properties vary with each load of the page. -->
<div id="___ytsubscribe_0" otherproperties="notmentionedhere">
    <iframe otherproperties="notmentionedhere">
        #document <!-- This element doesn't really exist, it's Firefox's way of representing the markup inside the iFramed page in DevTools -->
            <!--some other elements-->
                <button data-channel-external-id="UCO6YRllfXXIe2lPWenjmfPw" otherproperties="notmentionedhere"><!-- some other elements /--></button>
            <!--/some other elements-->
</div>

我的当前代码在span的onlick属性中单击该按钮:

document.getElementById('___ytsubscribe_0').querySelector('iframe').contentWindow.document.querySelector('button[data-channel-external-id=UCO6YRllfXXIe2lPWenjmfPw]').click();

我遇到的问题是,元素的ID属性必须以字母开始,但YouTube订阅按钮的容器ID以三个下划线开头。
下面的代码片段显示了实际的标记和我的代码:

<!-- You might have to copy the markup into an own HTML document on your computer to see it in action, at least for me it always fails on load with a "SecurityError: Document.cookie getter: Forbidden in a sandboxed document without the 'allow-same-origin' flag."... -->
<span onclick="document.getElementById('___ytsubscribe_0').querySelector('iframe').contentWindow.document.querySelector('button[data-channel-external-id=UCO6YRllfXXIe2lPWenjmfPw]').click();">Click here to subscribe!</span>
<script src="https://apis.google.com/js/platform.js"></script>
<div class="g-ytsubscribe" id="yt-sub-dark" data-channelid="UCO6YRllfXXIe2lPWenjmfPw" data-layout="full" data-theme="dark" data-count="default"></div>

注意:it's a seperate question to click on a button inside a CrossOrigin-iFrame。这个问题只是关于查找id以下划线开头的。

相关问题