Javascript 在循环中插入后面的链接
我需要在WordPress循环中插入锚。我试图用JavaScript(我对JavaScript很陌生)将其归档,如下所示:Javascript 在循环中插入后面的链接,javascript,jquery,wordpress,class,insertafter,Javascript,Jquery,Wordpress,Class,Insertafter,我需要在WordPress循环中插入锚。我试图用JavaScript(我对JavaScript很陌生)将其归档,如下所示: document.getElementByClass("post-40").insertAfter("<a href='#anker1'>\f107</a>"); document.getElementByClass(“post-40”).insertAfter(“”); 我想存档的结果是- 箭头是FontAwesome中的图标- 如果有人能帮我
document.getElementByClass("post-40").insertAfter("<a href='#anker1'>\f107</a>");
document.getElementByClass(“post-40”).insertAfter(“”);
我想存档的结果是-
箭头是FontAwesome中的图标-
如果有人能帮我就好了。请对我温柔一点,我对代码很陌生 使用以下命令添加锚定标记:
jQuery(document).ready(function($) {
$('.post-40').after('<a href="#anker1"></a>');
});
我的示例将起作用,并且是在回答您关于在JS/jQuery中执行此操作的问题时提供的,但是这不是最好的方法。添加锚定标记确实应该在PHP中完成。为什么不使用WordPress/PHP呢
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="entry"> <!-- or whatever your loop code is... -->
<?php the_content(); ?>
</div>
<?php if ( $post->ID == "40" ) : ?>
<a href='#anker1'>\f107</a>
<?php endif; ?>
<?php endwhile; else: ?>
更多信息:insertAfter
是一个jQuery函数。您不能在DOM元素上使用它。有没有办法用JavaScript归档所需的结果?这非常有用!谢谢是否可以添加更多ID,如?我能把一个类添加到a标签上吗?我可以用它来工作!谢谢你帮我工作!你救了我!
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="entry"> <!-- or whatever your loop code is... -->
<?php the_content(); ?>
</div>
<?php if ( $post->ID == "40" ) : ?>
<a href='#anker1'>\f107</a>
<?php endif; ?>
<?php endwhile; else: ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="entry"> <!-- or whatever your loop code is... -->
<?php the_content(); ?>
</div>
<?php
$myIDs = array(40, 42, 147, 256);
if ( in_array($post->ID, $myIDs) ) : ?>
<a href='#anker1' class="button-link link-<?php the_ID() ?>">\f107</a>
<?php endif; ?>
<?php endwhile; else: ?>