简体   繁体   English

使用php发送新帖子,但JavaScript不起作用

[英]Send a new post with php but JavaScript does not work

I'm sorry for bad english. 对不起,英语不好。 I am preparing a wall like facebook. 我正在准备像Facebook这样的墙。 Everything is working properly. 一切工作正常。 But I have a problem at delete button. 但是我在删除按钮上有问题。 Old ships delete button works fine. 旧船删除按钮可以正常工作。 However, when I create a new post. 但是,当我创建新帖子时。 Delete button does not work. 删除按钮不起作用。 but when I refresh the page it works. 但是当我刷新页面时,它可以工作。 I'm using JavaScript code. 我正在使用JavaScript代码。 You can find my code in question. 您可以找到我有问题的代码。 To understand me better i made a jpg file : JPG File PHP and Javascript : 为了更好地理解我,我制作了一个jpg文件: JPG File PHP和Javascript:

<?php
foreach($updatesarray as $data)
 {
 $msg_id=$data['msg_id'];
 $orimessage=$data['message'];
 $message=tolink(htmlentities($data['message']));
  $time=$data['created'];
   $username=$data['username'];
 $uid=$data['uid_fk'];
 $face=$Wall->Gravatar($uid);
 $commentsarray=$Wall->Comments($msg_id);
?>
<script type="text/javascript">
$(document).ready(function()
{
    $("._link").click(function(event)
    {
        var $gonderibody = $(event.target).closest(".gonderibody");
        if($(".bubset", $gonderibody).css('display')=='none')
        {
            $(".u_p_a", $gonderibody).addClass('open');
            $(".bubset", $gonderibody).css('display','block');


        }
        else
        {
             $(".bubset", $gonderibody).css('display','none');
             $(".u_p_a", $gonderibody).removeClass('open');

        }

    });
    $(document).click(function(e)
    {
      if($(e.target).attr('class')!='_link')
      {

        if($(".bubset").css('display')=='block')
        {
            if($('.u_p_a').hasClass('open'))
              {
                  $('.u_p_a').removeClass('open');
              }
              $(".bubset").css('display','none');
        }
      }
});

});
</script>
<script type="text/javascript"> 
$(document).ready(function(){$("#stexpand<?php echo $msg_id;?>").oembed("<?php echo  $orimessage; ?>",{maxWidth: 400, maxHeight: 300});});
</script>

<div class="gonderibody" id="gonderibody<?php echo $msg_id;?>">

<div class="u_p_a">
  <a class="_link"></a>
    <div class="bubset">
            <ul class="root">
                <li><a class="stdelete" href="#" id="<?php echo $msg_id;?>" title="Delete update">Sil</a></li>
            </ul>
        </div>
</div>
  <div class="gonderen_resim_zaman_isim">
    <div class="gonderen_resmi">
      <img src="<?php echo $face;?>" class='big_face'/>
    </div>
    <div class="gonderen_adi"><?php echo $username;?>Mustafa öztürk</div>
    <div class="gonderim_zamani"><?php time_stamp($time);?>birkaç dakika önce</div>
  </div>
  <div class="gonderi"><?php echo $message;?></div>
  <div class="begen_yorum_paylas">
    <div class="u_byp"><a href="#">Beğen</a></div>
    <div class="u_byp"><a href='#' class='commentopen' id='<?php echo $msg_id;?>' title='Comment'>Yorum yap </a></div>
    <div class="u_byp"><a href="#">Paylaş</a></div>
  </div>

<div id="stexpandbox">
<div id="stexpand<?php echo $msg_id;?>"></div>
</div>

<div class="commentcontainer" id="commentload<?php echo $msg_id;?>">


<?php include('load_comments.php') ?>





</div>
<div class="commentupdate" style='display:none' id='commentbox<?php echo $msg_id;?>'>
<div class="stcommentimg_mini">
<img src="<?php echo $cface;?>" class='small_face'/>
</div> 
<div class="stcommenttext" >
<form method="post" action="">

<textarea name="comment" class="comment"  id="ctextarea<?php echo $msg_id;?>"></textarea>
<br />
<input type="submit"  value=" Comment "  id="<?php echo $msg_id;?>" class="comment_button"/>
</form>


</div>
</div>


</div> 




<?php

  }
?>

There is more then one Problem with your code. 您的代码不止一个问题。

  1. id attributes in html DOM need to be UNIQUE. html DOM中的id属性必须是唯一的。 Don't give your id=msg_id and your delete link id=msg_id 不要提供您的id = msg_id和删除链接id = msg_id

  2. the event handler will only be attached to DOM objects already on the page. 事件处理程序将仅附加到页面上已有的DOM对象。 if you add an element it will have no event handler attached to it. 如果您添加一个元素,它将没有附加的事件处理程序。 To add event handlers also to future DOM elements you need to use delegate event handlers. 要将事件处理程序也添加到将来的DOM元素中,您需要使用委托事件处理程序。 with jQuery that is easy. 使用jQuery很简单。

wrong: 错误:

    $('#yourEl').click( function (){
        //...code
    });

right: 对:

    $(document).on('click', '#yourEl', function(){
        //...code
    )};

use document if you want to look for all new elements in document. 如果要在文档中查找所有新元素,请使用document。 better use a smaller scope. 最好使用较小的范围。 maybe .u_p_a ? 也许.u_p_a?

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

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