简体   繁体   English

jQuery:使用嵌入在JavaScript变量中的div作为jQuery选择器

[英]jQuery: using a div embedded in a JavaScript variable as a jQuery selector

I have a jQuery custom scrollbar, and I invoke it like this: 我有一个jQuery自定义滚动条,我这样调用它:

<script>
    (function($){
        $(window).on("load",function(){
            $(".main_text,#C2,.png_container").mCustomScrollbar();
        });
    })(jQuery);

That works correctly for all of the page elements except .png_container, but unlike the other sections, that section is only used in a JavaScript variable that is used to substitute text in a placeholder ID, and I think that's where the problem is. 这对.png_container以外的所有页面元素均正常工作,但与其他部分不同,该部分仅在JavaScript变量中使用,该变量用于替换占位符ID中的文本,我认为这就是问题所在。

Here is how it's called from an "onclick" button event: 这是从“ onclick”按钮事件中调用它的方式:

<div class="main_text">
<div id="C2">Main Text</div>
</div>

if (type == 101) {
var X = "<header>First Section</header><br>A classic example of good form/<br><br>More information<ul type=\"circle\"><li>Element Point 1<br></li><li>Element Point 1</li></ul><i><span class=\"span_01\">So much better</i></span><br><br><div class=\"png_container\"><img class=\"png_format\" src=\"images/Element 001.png\"></div>"}

document.querySelector("#C2").innerHTML = X;}

The png_container has a separate set of scroll bars, but they are not replaced by the custom scroll bars (the other page sections do get the custom scroll bars). png_container有一组单独的滚动条,但是它们并没有被自定义滚动条替换(其他页面部分的确获得了自定义滚动条)。

Here is the relevant css: 这是相关的CSS:

.png_container{
    overflow: auto;
    overflow-y: auto;
    overflow-x: auto;
    height: 400px;
    width: 800px;
    border: 2px solid;
    border-color: green;
}

#C2{
    color:#DBDBDB;
     font-family: camphorW04-Thin,calibri,arial;
    font-size: 14pt;
    text-indent: 0px;
    width: auto;
    margin: auto;
    margin-left: 20px;
    margin-right: 250px;
}

So my question is: how can I replace the scroll bars on a section that is embedded in a JavaScript variable, as shown above? 所以我的问题是:如何替换嵌入JavaScript变量的部分上的滚动条,如上所示?

My research has found some similar questions, but none that answer this specific question, so I hope somebody knows the answer. 我的研究发现了一些类似的问题,但是没有一个回答这个特定的问题,所以我希望有人知道答案。 Thanks very much for any ideas. 非常感谢您的任何想法。

You initialize the mCustomScrollbar plugin on load this way: 您可以通过以下方式初始化mCustomScrollbar插件:

$(window).on("load",function(){
  $(".main_text,#C2,.png_container").mCustomScrollbar();
});

The two first selectors have matching elements at this moment. 此时,前两个选择器具有匹配的元素。 But there is no existing element to match the last selector since .png_container is appended on click. 但是由于单击后附加了.png_container ,因此没有与最后一个选择器匹配的元素。

So you can safely remove .png_container from the load handler... 因此,您可以安全地从加载处理程序中删除.png_container ...
And initialise mCustomScrollbar on .png_container when it exists. 和初始化mCustomScrollbar .png_container时,它的存在。

$(window).on("load",function(){
  $(".main_text,#C2").mCustomScrollbar();  // Remove .png_container
});

$(".something").on("click",function(){
  if (type == 101) {
    var X = "<header>First Section</header><br>A classic example of good form/<br><br>More information<ul type=\"circle\"><li>Element Point 1<br></li><li>Element Point 1</li></ul><i><span class=\"span_01\">So much better</i></span><br><br><div class=\"png_container\"><img class=\"png_format\" src=\"images/Element 001.png\"></div>"}
    document.querySelector("#C2").innerHTML = X;
    $(".png_container").mCustomScrollbar();  // Add this.
  }

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

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