简体   繁体   English

PHP函数使用javascript / jquery隐藏div

[英]PHP Function using javascript / jquery hiding div

I have a php function: 我有一个PHP函数:

function myfunc() {

//then I have a div...

echo '<div class="overlay">';
echo "<button onclick=\"$('.overlay').hide();\">Close</button>";
echo '</div>';

}

My problem is that when I click on the close button the div is not hiding. 我的问题是,当我单击关闭按钮时,div没有隐藏。

What I'm I doing wrong here? 我在这里做错了什么?

Avoid to hardcode javascript handlers and inline events inside the output of php code: do instead 避免在php代码的输出中对javascript处理程序和内联事件进行硬编码:改为

echo '<div class="overlay">';
echo "<button>Close</button>";
echo '</div>';

and previously insert in your page this code that detects a click on your button using event delegation 并先前在此页中插入此代码,该代码使用事件委托检测到您的按钮被点击

<script>
  $(document).on('click', '.overlay button', function() {
      $(this).parent().hide()
  });
</script>

尝试:

<button onclick="this.parentNode.style.display = 'none'; return false;">Close</button>

Try this code: 试试这个代码:

function myfunc() {

//then I have a div...

echo '<div class="overlay"  id="overlay" >';
echo "<button onclick=\"hide()\">Close</button>";
echo '</div>';

}
//using the javascript code:
function hide()
{
    document.getElementById("overlay").style.display="none";

}

try: 尝试:

$('.overlay button').live('click',function(
    $('.overlay').css({'display': 'none'});
));

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

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