简体   繁体   English

为什么我不能按ID(没有iframe)访问此动态加载的DOM元素?

[英]Why can't I access this dynamically loaded DOM element by ID (no iframes)?

I am working with an external site that allows embedded javascript content (Qualtrics). 我正在与允许嵌入JavaScript内容(Qualtrics)的外部站点合作。 Qualtrics dynamically loads some controllers. Qualtrics动态加载某些控制器。 When I test accessing these controllers via the Chrome web panel, after they have fully loaded, I can see the loaded controllers in the Elements window which represents the current DOM. 当我测试通过Chrome Web面板访问这些控制器时,在它们完全加载后,可以在表示当前DOM的Elements窗口中看到已加载的控制器。 However, I cannot access these elements by id, either with jQuery or via document.getElementById. 但是,我无法通过jQuery或通过document.getElementById通过id访问这些元素。

One of the controllers has the id QID12~14~handle . 其中一个控制器具有ID QID12~14~handle In the Elements DOM browser, I see the tag: 在Elements DOM浏览器中,我看到标记:

<div id="QID12~14~handle" class="handle selected" style="left: 122px;"></div>

When I view the page's source, I see they are dynamically loading and inserted into the page via a script tag: 当我查看页面的源代码时,我看到它们正在动态加载并通过脚本标签插入页面:

<div class='QuestionBody BorderColor'>
    <div class='horizontalbar ChoiceStructure RtlOverride'></div>
    <div id='debug'></div>

    <!-- This has to be called after the html it references. Thats why its down here -->

    <script type='text/javascript'>
        QModules.loadExternalModule('../WRQualtricsShared/JavaScript/CSBar/slider.51487.js', function () {
            (function () {
                CS_QID15 = new CSBar('0', '100', '10', 'QID15', '');
                if (CS_QID15.loaded) {
                    CS_QID15.setDecimals(0);
                    if (CS_QID15.snapToGrid) {
                        CS_QID15.makeSlider('QID15~1');
                        CS_QID15.makeSlider('QID15~2');

                        CS_QID15.setStartPositions({"1": 0, "2": 0, "3": 0.64599483204134});
                    }
                    else {
                        CS_QID15.makeSlider('QID15~1');
                        CS_QID15.makeSlider('QID15~2');

                        CS_QID15.setStartPositions({"1": 0, "2": 0, "3": 0.64599483204134});
                    }
                }
            }).delay(); //if ie is waiting for something and this loads too fast it breaks. the defer fixes a very esoteric bug.
                        });
    </script>

    <div class='clear zero'></div>
</div>

The page is not using iFrames. 该页面未使用iFrame。 If I see an id in the current DOM, why can't I access it by it's id as it currently exists in the DOM? 如果我在当前DOM中看到一个ID,为什么不能通过它当前在DOM中存在的ID来访问它?

If I call jQuery(".handle") , I see this element: 如果我调用jQuery(".handle") ,则会看到以下元素:

[
  <div id=​"QID12~14~handle" class=​"handle selected" style=​"left:​ 122px;​">​</div>​, 
  <div id=​"QID15~1~handle" class=​"handle selected" style=​"left:​ 0px;​">​</div>​, 
  <div id=​"QID15~2~handle" class=​"handle selected" style=​"left:​ 0px;​">​</div>​
]

What could prevent me from accessing these elements by id? 是什么可以阻止我通过id访问这些元素?

QID12~14~handle is not a valid selector (although it is a valid id attribute) because of the ~ , which are sibling selectors. QID12~14~handle不是有效的选择器(尽管它是有效的id属性),因为~是兄弟选择器。 You can get around this using the id attribute itself: 您可以使用id属性本身解决此问题:

[id='QID12~14~handle']

Or you can escape the selectors with backslashes (this will also work for querySelector ): 或者,您可以使用反斜杠对选择器进行转义(这也适用于querySelector ):

QID12\\~14\\~handle

You may also need # to indicate the ID selector depending upon the API you are working with. 您可能还需要#来指示ID选择器,具体取决于您使用的API。

Sorry, I realized I had a much simpler problem. 抱歉,我意识到我遇到了一个简单得多的问题。 The tilde (~) character must be escaped in jQuery. 代字号(〜)字符必须在jQuery中转义。 This query returns the correct element: 此查询返回正确的元素:

jQuery("#QID12\\~14\\~handle")

The tilde in the id seems to be the issue. id中的波浪号似乎是问题所在。

You need to escape the special character with a double back slash like this. 您需要像这样用双反斜杠来转义特殊字符。

$("#QID15\\~1")

I just tried the following in the console it it seemed to work 我只是在控制台中尝试了以下内容,它似乎可以正常工作

//add element for testing
$('body').append('<div id="QID15~1"></div>');

$("#QID15~1")
//returned a blank array

$("#QID15\\~1")
//returned the div as expected

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

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