javascript - 如何在 colorbox 中显示一个 div - Jquery 插件

标签 javascript jquery html css colorbox

我想在 Colorbox 插件中显示一个 div,但它不起作用,因为 div 元素中有很多双引号并且会导致问题,所以我该怎么做。

这是我的代码

<script>
        $(document).ready(function() {
        if(localStorage.getItem('welcomehelloState') != 'shown'){
        function openColorBox(){
                $.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: "<section class="related">
    <p>New Content:-</p>
    <a href="/part-2/index>
        <img src="img/related/Prdha.png" />
        <h3>Art</h3>
    </a>
    <a href="/part-2/index6">
        <img src="img/related/Kavi.png" />
        <h3>Games</h3>
    </a>
    </section>"});
                               }
        setTimeout(openColorBox, 100);
        localStorage.setItem('welcomehelloState','shown')
        };});
</script>

如您所见,有很多双引号,括号不同步。

最佳答案

问题是,当你用双引号 (") 开始一个字符串并在字符串中放置另一个引号时,JS 认为你结束了字符串。这就是为什么必须在字符串中的引号有要转义:\"

示例:var x = "En then he said:\"Hi there\", with a big smile";

此外,JS 不喜欢换行符。如果你非常需要它,你也必须转义它们,但更好的方法是将字符串分成多个字符串

例子:

var x = "this is" +
"A multiline" + 
"String.";

你的代码必须是这样的:

<script>
    $(document).ready(function() {
        if(localStorage.getItem('welcomehelloState') != 'shown'){
            function openColorBox(){
                $.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: "<section class=\"related\">" +
                "<p>New Content:-</p>" +
                "<a href=\"/part-2/index\">" +
                        "<img src=\"img/related/Prdha.png\" />" +
                        "<h3>Art</h3>" +
                        "</a>" +
                        "<a href=\"/part-2/index6\">" +
                        "<img src=\"img/related/Kavi.png\" />" +
                        "<h3>Games</h3>" +
                        "</a>" +
                        "</section>"});
            }
            setTimeout(openColorBox, 100);
            localStorage.setItem('welcomehelloState','shown')
        }
    });
</script>

关于javascript - 如何在 colorbox 中显示一个 div - Jquery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182594/

相关文章:

javascript - 使用过渡的 div 动画

javascript - 如何在 reactJS 中设置背景图片的属性

javascript - lodash中的一个数组映射函数

jquery - 如何使用chrome扩展访问localstorage

javascript - 使用 jQuery 从文件动态加载元素的 CSS + anchor 链接不起作用

javascript - 空 href 不适用于 javascript

javascript - Material UI 自定义主题颜色分配 Typescript

javascript - jQuery 绑定(bind)对象上的所有事件

javascript - Blade laravel 中的总值之和

javascript - 我的 parseFloat 简单 JavaScript 代码忽略了小数