简体   繁体   English

执行JavaScript,进入先前使用jQuery / JavaScript打开的div

[英]Execute JavaScript, into the divs opened previously with jQuery/JavaScript

I am doing a comment system. 我正在做一个评论系统。 Yesterday I asked for the same problem in stackoverflow and I didnt get any answer. 昨天我在stackoverflow中问了同样的问题,但没有得到任何答案。 Now I have simplificated the code, so I hope that its easier to understand. 现在,我已经简化了代码,所以希望它更容易理解。 I also write complete code if someone what to try it. 如果有人尝试,我也会编写完整的代码。

The main HTML page has multiple divs . HTML主页面具有多个div

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="http://code.jquery.com/jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="jscripts/actions.js"></script>
</head>

<body>
<div class="comments_div" id="comments_div" data-id="22" > 
  div_22--->
 <div class="comments_more_22" id="comments_more"> </div>
</div>
<br>
<div class="comments_div" id="comments_div" data-id="23" > 
  div_23--->
 <div class="comments_more_23" id="comments_more"> </div>
</div>
<br>
<div class="comments_div" id="comments_div" data-id="24" > 
  div_24--->
 <div class="comments_more_24" id="comments_more"> </div>
</div>
<br>
</body>

The jQuery/JavaScript functions open/hide the div and show the post_id (data-id). jQuery / JavaScript函数打开/隐藏 div并显示post_id(数据ID)。 Its works perfect!. 它的作品完美!

//Open div
$(function() {
  $(".comments_div").click(function() {
    var post_id = $(this).attr('data-id');       
    $.ajax({
      url: "jscripts/comment.php",
      type: "POST",
      data: { post_id: post_id },
      success: function(datos) {
        $('.comments_more_' + post_id).html(datos);
        $('.comments_more_' + post_id).show('slow');
        //alert(post_id);
      }
    });
  });
})


//Hide div
$(document).mouseup(function (e){
    var container = $('div[id^="comments_more"]');
    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {        container.hide('slow');    }
});

//Put text in some div
function showText()
{ document.getElementById("flash").textContent = 'works!';}

...and comment.php (here the code will be more complicated, with forms, captchas, etc) ...和comment.php (此处的代码会更加复杂,包括表单,验证码等)

<?php
    echo 'post_id: ';
    echo $_POST['post_id'];
?>

<div class="flash" id="flash">- </div>

<span style="margin-left:20px;"><small>click<a href='javascript: showText();'>here</a> </small></span><br/>

Nice, isn't it?. 很好,不是吗? But I need to execute another JavaScript: showText() . 但是我需要执行另一个JavaScript:showText() This script works when you click in this order: div_24, div_23, div_22. 当您按以下顺序单击时,此脚本有效:div_24,div_23,div_22。 But stop works when you try click first div_22, then div_23,... etc. 但是,当您尝试先单击div_22,然后单击div_23等时,则停止工作。

Well I solved the problem: the div "flash" must have a unique id, so showText can find the correct div. 好吧,我解决了这个问题:div“ flash”必须具有唯一的ID,以便showText可以找到正确的div。

<?php
    echo 'post_id: ';
    echo $_POST['post_id'];
$post_id=$_POST['post_id'];
?>

<div class="flash" id="flash_<?php echo $post_id; ?>">- </div>

<span style="margin-left:20px;"><small>click<a href='javascript: showText();'>here</a> </small></span><br/>


<script type="text/javascript"> 
//Put text in some div
function showText()
{
var post_id = "<?php echo $post_id; ?>" ;
document.getElementById("flash_" + post_id).textContent = 'works!';
}

</script>

:P :P

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

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