Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果单击另一个div,请关闭弹出窗口_Javascript_Php_Jquery_Popup - Fatal编程技术网

Javascript 如果单击另一个div,请关闭弹出窗口

Javascript 如果单击另一个div,请关闭弹出窗口,javascript,php,jquery,popup,Javascript,Php,Jquery,Popup,我有一个弹出窗口,使用以下代码打开: 更新: <ul class="grid cs-style-1"> <?php $explainname = new wp_query( array ( 'post_type' => 'explainname' ) ); if ( $explainname->have_posts() ) : while ( $explainname->have_posts() ) : $explainname-

我有一个弹出窗口,使用以下代码打开:

更新

<ul class="grid cs-style-1">
    <?php 
    $explainname = new wp_query( array ( 'post_type' => 'explainname' ) );  
    if ( $explainname->have_posts() ) : while ( $explainname->have_posts() ) : $explainname->the_post();
    $explainname_icon = get_post_meta( $post->ID,'explainname_icon', true );
    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    $imageContent = get_the_content();
    $stripped = strip_tags($imageContent, '<p> <a>'); //replace <p> and <a> with whatever tags you want to keep after the strip

    ?>          
            <li class="desktop">
                <figure>
                    <img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
                    <figcaption>
                        <h3><?php the_title(); ?></h3>
                        <span style="text-align:right;font-size: 15px;"><?php echo $stripped; ?></span>
                    </figcaption>
                </figure>
            </li>
            <li class="popup" onclick="myFunction<?php echo $post->ID; ?>()">
                <figure>
                    <img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
                    <span class="popuptext" id="myPopup<?php echo $post->ID; ?>"><?php echo $stripped; ?></span>
                </figure>
            </li>
            <script>
            // When the user clicks on <div>, open the popup
            function myFunction<?php echo $post->ID; ?>() {
            var popup<?php echo $post->ID; ?> = document.getElementById("myPopup<?php echo $post->ID; ?>");
            popup<?php echo $post->ID; ?>.classList.toggle("show");
            }

            </script>

    <?php 
    endwhile; endif;
    wp_reset_query();
    ?>                  
</ul>
  • “alt=”“>
  • 用这个例子

    <script>
        $(document).not("#myPopup<?php echo $post->ID; ?>").click(function() {
            $('#myPopup<?php echo $post->ID; ?>').hide();
        });
    </script>
    
    
    $(文档)。不是(“myPopup”)。单击(函数(){
    $('#myPopup').hide();
    });
    
    这里有一个解决方案。当你点击一个DIV时,它会关闭你的弹出窗口。如果你想锁定所有弹出窗口,那么你需要给它们一个通用的类名

    document.getElementsByTagName('div').onclick = function(e){
        document.getElementById("myPopup<?php echo $post->ID; ?>").style.display = 'none';
    
    });
    
    document.getElementsByTagName('div')。onclick=function(e){
    document.getElementById(“myPopup”).style.display='none';
    });
    
    只需在javascript中使用

    var popup<?php echo $post->ID; ?> = document.getElementById("myPopup<?php echo $post->ID; ?>");
    window.onclick = function() {
        if (popup.classList.contains("show")) {
            popup.classList.toggle("show");
        }
     }
    
    var popup=document.getElementById(“myPopup”);
    window.onclick=function(){
    if(popup.classList.contains(“show”)){
    popup.classList.toggle(“显示”);
    }
    }
    
    selecteddiv的意思是什么?我没有名为selecteddiv的div。现在将此代码放在哪里检查它添加到脚本之后我们都在猜测。没有HTML,无法测试这一点。你能发布弹出窗口的HTML及其所在页面吗?否则我认为你找不到有用的答案。请参阅我的更新,我在上面添加代码