0

请编辑我的代码以使其正常工作。

为最新版本编辑。

这是我所拥有的:

<body>
<!-- visibility toggle -->
<script type="text/javascript">
<!--
function toggle_visibility()
    {
       if(document.getElementById(window.event.srcElement.id+'menu').style.display=='block'){
            document.getElementById(window.event.srcElement.id+'menu').style.display='none';
        }
        else{ 
            document.getElementById(window.event.srcElement.id+'menu').style.display='block';
        }
    };
//-->
</script>

这是我的 div(经过编辑以准确显示我所拥有的)

<ul class="lyrics"><h3>ALL LYRICS</h3>
    <?php while ( have_posts() ) : the_post(); ?>   
        <li ><a id="links" href="#" onclick="toggle_visibility();"><?php the_title(); ?></a>
            <div id="linksmenu"><?php the_content();?></div>
        </li>
    <?php endwhile; // End the loop. Whew. ?>
</ul>

以下是发生的情况:无论我单击哪个链接,都只会显示与最后一个“the_content”相关联的文本。

这是我需要的:最初所有的“子”div 都不可见。当我单击“标题 1”链接时,“子文本 1”将变为可见。当我单击“标题 2”链接时,“子文本 2”将变为可见,“子文本 1”将变为不可见。

这将在 WordPress 博客中,因此标题 div 的数量会发生变化。永远只有一个孩子。

提前致谢!

4

3 回答 3

0

It is doing this because you are calling the_content() twice. I am assuming your file looks like:

<?php get_header();
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<ul> 
<li class="main"><a href="#" title="Title 1"><?php the_title(); ?></a>   <div class="child"><?php the_content(); ?></div> </li> 
<li class="main"><a href="#" title="Title 2"><?php the_title(); ?></a>   <div class="child"><?php the_content(); ?></div> </li>  <ul> 
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

You need to have two loops, but I can't supply detail without seeing more code. Read about multiple loops.

于 2012-06-28T13:58:47.887 回答
0

div无论您单击何处,都会切换第一个的原因是因为您使用了硬编码的 id foo。传递thistoggle_visibility而不是文字'foo'和内部toggle_visibility函数,找到要切换的 div(这将是传递参数的第一个子项)。

于 2012-06-28T15:04:23.013 回答
0

丢弃参数并使用触发事件的元素的 id 构建要设置为可见/不可见的 id。

 function toggle_visibility()
        {
            if(document.getElementById(window.event.srcElement.id+'menu').style.display=='block'){
                document.getElementById(window.event.srcElement.id+'menu').style.display='none';
            }
            else{ 
                document.getElementById(window.event.srcElement.id+'menu').style.display='block';
            }
        };

只要你想展示的东西总是有 id+'menu' 之类的,你就可以用函数来展示它。(注意父母有 idlinks而孩子有 id linksmenu

<ul class="lyrics"><h3>ALL LYRICS</h3>
    <?php while ( have_posts() ) : the_post(); ?>   
    <li ><a id="links" href="#" onclick="toggle_visibility();"><?php the_title(); ?></a>
      <div id="linksmenu"><?php the_content();?></div>
    </li>
    <?php endwhile; // End the loop. Whew. ?>
</ul>
于 2012-06-28T15:09:32.267 回答