【问题标题】:How to use JavaScript with repeated elements with same ids in one page?如何在一页中使用具有相同 id 的重复元素的 JavaScript?
【发布时间】:2017-05-14 11:07:45
【问题描述】:

我正在构建一个 wordpress 网站,但我在其中一个页面中遇到了 JavaScript 实现问题。

我在我的网站中使用了一个用于问答功能的插件。我想为此页面中的每个用户答案添加星级。我在 HTML 和 JavaScript 中添加了一个 div 来更改这个 div 以根据从数据库中获取的用户的星值显示星。

我遇到的问题是:在我将代码(div 和 JavaScript)添加到这个插件 PHP 文件(显示问题答案插件的答案)之后,只有第一个用户的答案星值被更改。星显示的其余部分显示满 5 星。

如何处理同一页面中相同id的重复元素?

这是插件 php 文件的一部分(显示问答插件的答案)

<div id="post-<?php the_ID(); ?>" <?php post_class() ?> ap-id="<?php 
the_ID(); ?>" ap="answer">
<div class="ap-content" itemprop="suggestedAnswer<?php echo ap_is_selected() 
? ' acceptedAnswer' : ''; ?>" itemscope 
itemtype="https://schema.org/Answer">
<div class="ap-single-vote"><?php ap_vote_btn(); ?></div>
<div class="ap-avatar"> 
<a href="<?php ap_profile_link(); ?>"<?php ap_hover_card_attr(); ?>>
<?php ap_author_avatar( ap_opt( 'avatar_size_qanswer' ) ); ?>
</a>
</div>
<div class="ap-cell clearfix">
<div class="ap-cell-inner">
<div class="ap-q-metas">
<?php echo ap_user_display_name( [ 'html' => true ] ); ?>
<a href="<?php the_permalink(); ?>" class="ap-posted">
<time itemprop="datePublished" datetime="<?php echo ap_get_time( 
get_the_ID(), 'c' ); ?>">
<?php printf( 'Posted %s', ap_human_time( ap_get_time( get_the_ID(), 'U' ) ) 
 ); ?>
</time>
 </a>
 <?php ap_recent_post_activity(); ?>
 <?php echo ap_post_status_badge( ); // xss okay.   ?>
  </div>

  <div class="ap-q-inner">
  <?php  //star rating data fetch from database.
   $_postx = ap_get_post();
   $curru_ID=$_postx->post_author;
   $answertitle=get_the_title();


   $con=mysqli_connect("localhost","root","","sitedata");
   $ratevalue1=mysqli_query($con,"select rating from ratings where 
   Name='$answertitle' AND postID='$curru_ID'");
   $ratevaluearr1 = mysqli_fetch_array($ratevalue1);
   $ratingonequery=$ratevaluearr1[rating];
   $ratenamevalue1=mysqli_query($con,"select ratingname from ratings where 
   Name='$answertitle' AND postID='$curru_ID'");
   $ratenamevaluearr1 = mysqli_fetch_array($ratenamevalue1);
   $ratingnameonequery=$ratenamevaluearr1[ratingname];
    ?>
    <?php 
    do_action( 'ap_before_answer_content' );
    ?>
    <div class="ap-answer-content ap-q-content" itemprop="text" ap-content>
    <?php the_content(); ?>
    </div>
    <?php
    do_action( 'ap_after_answer_content' );
    ?>

    </div>

    <div id="rate1" class="rating"></div>   
    <?php if ( ap_user_can_read_answer( ) ) : ?>
    <div class="ap-post-footer clearfix">
    <?php echo ap_select_answer_btn_html( ); // xss okay ?>
    <?php ap_post_actions_buttons() ?>
    <?php echo ap_comment_btn_html(); // xss okay. ?>
    </div>
    <?php endif; ?>
    </div>
    <?php ap_the_comments(); ?>
    </div>
    </div>
    </div>
    <script>
    var cw = window.rate1.clientWidth; // save original 100% pixel width
    var userstarvalue = "<?php echo $ratingonequery ?>";
    rating(userstarvalue);
    function rating( stars ) {
    var ratingfill=stars;
       var rating_integer=Math.floor(ratingfill);
       var rating_decimal=ratingfill%1;
       var rating_dec_trimmed=rating_decimal.toFixed(1);  
       if((rating_dec_trimmed==0.1)||(rating_dec_trimmed==0.2)||
          (rating_dec_trimmed==0.3)||(rating_dec_trimmed==0.4))
       {
             window.rate1.style.width = ((40*rating_integer)+18) + 'px';
       }
       if((rating_dec_trimmed==0.6)||(rating_dec_trimmed==0.7)||
          (rating_dec_trimmed==0.8)||(rating_dec_trimmed==0.9))
       {
             window.rate1.style.width = ((40*rating_integer)+28) + 'px';
       }
       if(rating_dec_trimmed==0.5)
       {
             window.rate1.style.width = ((40*rating_integer)+20) + 'px';
       }
       if(rating_dec_trimmed==0)
       {
             window.rate1.style.width = (40*rating_integer) + 'px';
       }
  } 
   </script>    
   <style>
   .rating {
   font-size: 48px;
   color: #0095f9;
   display: inline-block;
   overflow: hidden;
      }
.rating::before { 
   content: "\2605\2605\2605\2605\2605" 
}
   </style>

【问题讨论】:

  • 你不能有超过 1 个元素的相同 id
  • 可以,只是不习惯。

标签: javascript php jquery html css


【解决方案1】:

首先,多个元素不应具有相同的id,而应具有相同的class。它不会抛出错误,但不是约定。

您可以使用jQueryeach 功能遍历所有具有相同ID 或类的事件。

$('.rating').each(function(index, object) {
    //logic here
});

'index' 是数组中的位置,'object' 是 DOM 对象(类 rating)。

【讨论】:

  • .rating 表示 class 称为 rating 而不是 id
  • 是的,在他最初的问题中,他提到了id,所以只是澄清一下。
  • 您已经编写了代码来对评级类中的每个元素执行相同的逻辑。但这里情况并非如此。每个用户将具有从数据库中获取的用户给出的不同星级值。Answer.php(有问题的代码)在插件中用于在一页中显示每个用户的答案。
  • 当我使用 javascript 时,只有 1 个 id 被操纵,其余具有相同 id 的类似重复元素没有响应 javascript。
  • @mrinalmech,我没有明白你在回答中的意思。你能用你的代码展示如何在我的代码中实现这一点吗?你能展示如何完全做到吗?
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2018-09-09
  • 2011-04-06
相关资源
最近更新 更多