2016-05-14 2 views
0

Ich versuche, ein onclick-Ereignis auf div von jedem Beitrag in der Kategorie mit Hilfe von jquery auszuführen. Wie kann ich es ausführen?Wie man jquery innerhalb von category.php ausführt, was für jeden einzelnen Beitrag in der Kategorie wordpress funktionieren sollte

zuweisen I den Wert von div id dynamisch als answer_postid beispielsweise post 1 01 Post-ID hat so seinen div id = "answer_01" und post 2. hat Post-ID 02, also sein div id = "answer_02"

Jetzt möchte ich all divs jede Post innerhalb der Kategorie verbergen, wenn Dokument vollständig geladen und will auch mit der ID viewanswer_postid verschiedenen Posten enthalten <a> mit unterschiedlicher ID als viewanswer_o1, Onclick-Ereignis auf einem Element a auszuführen viewanswer_02 ......

Ich habe versucht, indem Sie Skript in content.php
nicht funktioniert.

<script> 
$(document).ready(function() { 
$("div#answer_<?php echo get_the_ID();?>").hide();  
}); 
</script> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
<header class="entry-header page-header"> 
    <h1 class="entry-title"><?php the_title(); ?></h1> 

    <div class="entry-meta text-muted"> 
     <?php ipt_kb_posted_on(); ?> 
    </div><!-- .entry-meta --> 
</header><!-- .entry-header --> 

<div class="entry-content"> 
    <?php the_content(); ?> 
    <table class="mcqtable"> 
     <tbody> 
      <tr> 
       <td class="index"><?php echo '<a id="index_A_'.get_the_ID().'" href="javascript: void 0;">A.</a>';?> 
       <td class="option"><?php echo get_post_meta($post->ID, 'option_A', true); ?></td> 
       <td class="index"><?php echo '<a id="index_B_'.get_the_ID().'" href="javascript: void 0;">B.</a>';?> 
       <td class="option"><?php echo get_post_meta($post->ID, 'option_B', true); ?></td> 
      </tr> 
      <tr> 
       <td class="index"><?php echo '<a id="index_C_'.get_the_ID().'" href="javascript: void 0;">C.</a>';?> 
       <td class="option"><?php echo get_post_meta($post->ID, 'option_C', true); ?></td> 
       <td class="index"><?php echo '<a id="index_D_'.get_the_ID().'" href="javascript: void 0;">D.</a>';?> 
       <td class="option"><?php echo get_post_meta($post->ID, 'option_D', true); ?></td> 
      </tr> 
     </tbody> 
    </table> 
    <a class="view_answer_<?php echo get_the_ID(); ?>" href="javascript: void 0;">View Answer</a> 
    <div id="answer_<?php echo get_the_ID();?>"> 
     <?php echo 'Correct Answer is: '.get_post_meta($post->ID, 'option_correct', true); ?> 
    </div> 
    <?php 
     wp_link_pages(array(
      'before' => __('<p class="pagination-p">Pages:</p>', 'ipt_kb') . '<ul class="pagination">', 
      'after' => '</ul><div class="clearfix"></div>', 
     )); 
    ?> 
</div><!-- .entry-content --> 

<footer class="entry-meta text-muted well well-sm"> 
    <?php 
     /* translators: used between list items, there is a space after the comma */ 
     $category_list = get_the_category_list(__(', ', 'ipt_kb')); 

     /* translators: used between list items, there is a space after the comma */ 
     $tag_list = get_the_tag_list('', __(', ', 'ipt_kb')); 

     if (! ipt_kb_categorized_blog()) { 
      // This blog only has 1 category so we just need to worry about tags in the meta text 
      if ('' != $tag_list) { 
       $meta_text = __('This entry was tagged <i class="glyphicon glyphicon-tags"></i>&nbsp;&nbsp;%2$s. <br />Bookmark the <i class="glyphicon glyphicon-bookmark"></i>&nbsp;&nbsp;<a href="%3$s" rel="bookmark">permalink</a>.', 'ipt_kb'); 
      } else { 
       $meta_text = __('Bookmark the <i class="glyphicon glyphicon-bookmark"></i>&nbsp;&nbsp;<a href="%3$s" rel="bookmark">permalink</a>.', 'ipt_kb'); 
      } 

     } else { 
      // But this blog has loads of categories so we should probably display them here 
      if ('' != $tag_list) { 
       $meta_text = __('This entry was posted in <i class="glyphicon glyphicon-folder-open"></i>&nbsp;&nbsp;%1$s and tagged <i class="glyphicon glyphicon-tags"></i>&nbsp;&nbsp;%2$s. <br />Bookmark the <i class="glyphicon glyphicon-bookmark"></i>&nbsp;&nbsp;<a href="%3$s" rel="bookmark">permalink</a>.', 'ipt_kb'); 
      } else { 
       $meta_text = __('This entry was posted in <i class="glyphicon glyphicon-folder-open"></i>&nbsp;&nbsp;%1$s. <br />Bookmark the <i class="glyphicon glyphicon-bookmark"></i>&nbsp;&nbsp;<a href="%3$s" rel="bookmark">permalink</a>.', 'ipt_kb'); 
      } 

     } // end check for categories on this blog 

     printf(
      $meta_text, 
      $category_list, 
      $tag_list, 
      get_permalink() 
     ); 
    ?> 

    <?php edit_post_link(__('Edit', 'ipt_kb'), '<span class="edit-link"><i class="glyphicon glyphicon-edit"></i>&nbsp;&nbsp;', '</span>'); ?> 
</footer><!-- .entry-meta --> 

Antwort

0

Ist der jQuery-Code innerhalb der Schleife?

Das ist keine sehr gute Technik.

Warum ein class="answer" zu jeder Antwort div nicht zuordnen und dann

$(document).ready(function() { 
    $('div.answer').each(function(){ 
     $(this).hide(); 
    }); 
}); 

außerhalb der Schleife verwenden?

Auch wäre es hilfreich, wenn Sie ein einfaches HTML-Beispiel geschrieben, um genau zu sehen, was falsch ist (jedes Thema eine andere Struktur hat, so sagen content.php nicht viel nützlich ist). Hoffe das hilft.

Verwandte Themen