jQuery从while循环中获取值,并在没有单击的情况下将其显示在另一个div中

How do I make jQuery get value from while loop and display it in another div?

I know I should do it with PHP, But I want to display it with only jQuery.

<? while($commentresult = $commentdata->fetch_array(MYSQLI_ASSOC)) { ?>

    <div class="show_url"></div>
    <a href="<? echo $commentresult['comment']; ?>" class="get_url_comments" target="_blank"></a>

<? } ?>

$(document).ready(function() {
    var getUrl = $('.get_url_comments').attr('href');
    $(".show_url").text("Click This" + getUrl);
});

To do this you can set the text() of the .show_url element based on the following .get_url_comments. Try this:

$(document).ready(function() {
    $(".show_url").text(function() {
        return $(this).next('.get_url_comments').attr('href');
    });
});

You should note though that it would be a much better idea to do this directly in PHP:

<? while($commentresult = $commentdata->fetch_array(MYSQLI_ASSOC)) { ?>
    <div class="show_url"><? echo $commentresult['comment']; ?></div>
    <a href="<? echo $commentresult['comment']; ?>" class="get_url_comments" target="_blank"></a>
<? } ?>

Try with this..

$(document).ready(function() {
    $('.get_url_comments').map(function(guc){
         var el = $(guc);
         var url = el.attr('href');
         el.prev().text("Click This" + url);
         // or
         el.prev().html("Click This" + url);
    });
});