【问题标题】:Close popup simple modal [closed]关闭弹出简单模式[关闭]
【发布时间】:2014-12-22 14:45:52
【问题描述】:

我正在使用简单的模式来创建一个弹出窗口,我尝试添加将关闭弹出窗口的按钮,但是当我单击它时没有任何反应。

  1. 附上示例模型的js库

下面是 jQuery 代码

jQuery('a.postpopup').click(function(){
        id = jQuery(this).attr('rel');
url='http://localhost/website/?page_id=81';
        jQuery('<div id="ajax-popup"></div>').hide().appendTo('body').load(url+'&id='+id).modal();
        return false;
    });
//btn to close the popup
jQuery('#btnclose').click(function(){
alert('hello'); // close code should be placed here but alert didn't executed.
  });

下面是我的模板页面中的代码(弹出)

<?php
/*
Template Name: my template
*/
?>
<?php
    $post = get_post($_GET['id']);
?>
<?php if ($post) : ?>
    <?php setup_postdata($post); ?>
    <div class="whatever">
    <div id="popheader">
         <img id="popimg" src="http://localhost/website/wp-content/uploads/2014/12/logo1.png" width="200" height="auto"/>
         <font id="popup-title" class="entry-title" > <?php the_title()?> </font>
         <input type="button" id="btnclose" /> //btn to close the pop
    </div>
        <table class="tblcontent">
        <tr>
            <td id="popup-content">
                <?php the_content(); ?>
            </td>
        </tr>
        </table>        
    </div>
<?php endif; ?>

那么,我怎样才能关闭这个弹出窗口

【问题讨论】:

  • 在您尝试绑定此点击事件后,此元素(动态?)是否添加到 DOM 中?如果您搜索十万个重复问题...如果没有,请提供相关示例以复制您的问题

标签: jquery wordpress simplemodal


【解决方案1】:

因为#btnclose 还不存在。加载 ajax 内容时,您应该绑定事件,仅在加载内容之后。

jQuery('a.postpopup').click(function(){
    id = jQuery(this).attr('rel');
    url='http://localhost/website/?page_id=81';
    jQuery('<div id="ajax-popup"></div>')
        .hide()
        .appendTo('body')
        .load(url+'&id='+id, function(data){
            jQuery(data).find('#btnclose').click(function(e){
                alert("Button clicked");
            });
        })
        .modal();

    return false;
});

或者修改你的模板并将函数附加到按钮点击:

<input type="button" id="btnclose" onclick="doSomething()" /> //btn to close the pop

function doSomenthing() {
    alert("Button Clicked");
}

【讨论】:

  • ,非常感谢,但我尝试通过 .find 方法进行第一个解决方案,但什么也没发生,客户端浏览器中也没有出现错误,你知道吗?? ........第二种修改模板的方法效果很好。
  • 或者您可以使用初始解决方案并将您的 js 更改为 jQuery('#btnclose').on('click',function(){ //alert }); "on" 方法监听新创建的元素
  • @antoineguillien,很好,谢谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多