简体   繁体   English

post()中的jQuery函数不选择父函数的元素

[英]jQuery function in post() not selecting elements of parent function

I have a javascript function, which should hide a user post on call, and then should change the button to call the function doing the opposite (unhide) and display a corresponding changed text. 我有一个javascript函数,它应该在调用时隐藏用户帖子,然后应该更改按钮以调用相反的功能(取消隐藏)并显示相应的更改文本。

function unhidePost(postid) {
  if( called ) return false;
  called = true;
  this.id="tempID";
  $.post("../scripts/requests.php", {
    visiblePost: 1,
    postId: postid,
  }, function(success){
    if (success) {
      $("#tempID").html("<i class=\"fas fa-eye-slash\"></i>&nbsp;Hide");
      $("#tempID").attr("onclick", "hidePost(" + postid + ")");
    }
  });
  reload();
}

The post request gets send, the .php then sends a query request and returns 1 or 0, depending on success. post请求被发送,然后.php发送查询请求并返回1或0,具体取决于成功。 Then it should change the html with AJAX. 然后它应该用AJAX更改html。 First I tried this a selector, which selected the .php , so I gave the html element a temporary id ( this.id="tempID"; ) which works, but the selector inside the function ( "#tempID" ) can't find the element. 首先我尝试了this选择器,它选择了.php ,所以我给了html元素一个临时id( this.id="tempID"; ),但是函数中的选择器( "#tempID" )不能找到元素。

What is it trying to select then? 它试图选择什么呢? And how can I get it to select in the original document? 我怎样才能在原始文档中选择它?

Orig html (php), as requested: Orig html(php),根据要求:

function displayPost($entry, $class = "entry1"){
  $userid = $entry['userid'];
  $username = getUserName($userid);
  $created_at = DateTime::createFromFormat('Y-m-d H:i:s', $entry['created_at']);
  echo "<div class=\"".$class."\" onclick=\"postById(".$entry['id'].")\"><p class=\"info\">Posted by <a class=\"userlink\" onclick=\"userById(".$userid.")\">".$username."</a> on ".$created_at->format('j.n.Y')." at ".$created_at->format('H:i')."</p>";
  echo "<h2 class=\"title\">".$entry['title']."</h2>";
  echo $entry['content'];
  if(isset($_SESSION['userid']) && $entry['userid'] == $_SESSION['userid']){
    echo "<br><p class=\"info\">&nbsp;&nbsp;<a class=\"actionlink\" onclick=\"editPost(".$entry['id'].")\"><i class=\"far fa-edit\"></i>&nbsp;Edit</a></p>";
    echo "<p class=\"info\">&nbsp;&nbsp;<a class=\"actionlink\" onclick=\"deletePost(".$entry['id'].")\"><i class=\"far fa-trash-alt\"></i>&nbsp;Delete</a></p>";
    if(isset($entry['visible']) && $entry['visible'] == true){
      echo "<p class=\"info\">&nbsp;&nbsp;<a class=\"actionlink\" onclick=\"hidePost(".$entry['id'].")\"><i class=\"far fa-eye-slash\"></i>&nbsp;Hide</a></p>";
    } else {
      echo "<p class=\"info\">&nbsp;&nbsp;<a class=\"actionlink\" onclick=\"unhidePost(".$entry['id'].")\"><i class=\"far fa-eye\"></i>&nbsp;Unhide</a></p>";
    }
  }
  echo "</div>";
};

I hope you are aware concept of EventLoop inside the browser, javascript is single threaded and to make it async it uses the concept of EventLoop. 我希望你在浏览器中知道EventLoop的概念,javascript是单线程的,并使其异步使用EventLoop的概念。

Firstly in your code, there is 'Post' which requests php and success you are trying to access some elements from the DOM. 首先在你的代码中,有一个'Post'请求php和成功你试图从DOM访问一些元素。

There could be two possibilities : 可能有两种可能性:

  1. DOM is not completely loaded. DOM未完全加载。
  2. Element does not exist in the DOM tree. DOM树中不存在元素。

Please use a callback on completion/done of the 'POST' AJAX and write your functionality something like below code. 请在完成/完成“POST”AJAX时使用回调,并编写类似下面代码的功能。

var successObj;
$.ajax({
    url: url,   
    dataType: 'json',
    type: POST,
    data: 'specify your input',
    sucess:function(success){
       successObj = success;
    }
}).done(function() {
   if(successObj) {
       //YOUR CODE
   }
});

Okay having taken a look at your code, I can see the issue you are having. 好的,看了你的代码,我可以看到你遇到的问题。

if(isset($entry['visible']) && $entry['visible'] == true)
{
    print '<p class="info">&nbsp;&nbsp;<a class="actionlink" onclick="hidePost('.$entry['id'].')"><i class="far fa-eye-slash"></i>&nbsp;Hide</a></p>';
}
else
{
    print '<p class="info">&nbsp;&nbsp;<a class="actionlink" onclick="unhidePost('.$entry['id'].')"><i class="far fa-eye"></i>&nbsp;Unhide</a></p>';
}

Having changed your code slightly, it would appear that nowhere do you set an ID within your parent HTML you wish to change. 稍微更改了代码后,您似乎无法在您希望更改的父HTML中设置ID。 Therefore the jQuery selector looking for an ID "tempID" cannot be found. 因此,找不到寻找ID“tempID”的jQuery选择器。

I would change the PHP code to: 我会将PHP代码更改为:

if(isset($entry['visible']) && $entry['visible'] == true)
{
    print '<p class="info">&nbsp;&nbsp;<a id="post-'.$entry['id'].'" class="actionlink" onclick="hidePost('.$entry['id'].')"><i class="far fa-eye-slash"></i>&nbsp;Hide</a></p>';
}
else
{
    print '<p class="info">&nbsp;&nbsp;<a  id="post-'.$entry['id'].'" class="actionlink" onclick="unhidePost('.$entry['id'].')"><i class="far fa-eye"></i>&nbsp;Unhide</a></p>';
}

And then change the jQuery code to: 然后将jQuery代码更改为:

$("#post-"+postid).html("<a class=\"fas fa-eye-slash\"></i>&nbsp;Hide");
$("#post-"+postid).attr("onclick", "hidePost(" + postid + ")");

That should work much better than it is currently. 这应该比现在好得多。

Or you could simply change the jQuery, although I haven't tried it out, to: 或者您可以简单地更改jQuery,虽然我还没有尝试过,但是:

function unhidePost(postid) {

  if( called ) return false;

  called = true;

  $.post("../scripts/requests.php", {
    visiblePost: 1,
    postId: postid,
  }, function(success){
    if (success) {
      $(this).html("<i class=\"fas fa-eye-slash\"></i>&nbsp;Hide");
      $(this).attr("onclick", "hidePost(" + postid + ")");
    }
  });
  reload();
}

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

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