[英]How do I add a class to inner HTML of a div using Javascript?
I'm trying to work around a seemingly impossible customization for Wordpress. 我正在尝试解决Wordpress看似不可能的自定义问题。 Here is what
<?php the_category();?>
prints out: 这是
<?php the_category();?>
打印出来的:
<ul class="post-categories" >
<li>
<a rel="category-tag" title="..." href="...">Category One</a>
</li>
<li>
etc.
</li>
</ul>
I need to add / insert a class into all the <a>
's, to make it look like this: 我需要在所有
<a>
添加/插入一个类,以使其看起来像这样:
<ul class="post-categories" >
<li>
<a class="btn" rel="category-tag" title="..." href="...">Category One</a>
</li>
<li>
etc.
</li>
</ul>
So far, I've only found ways to assign additional classes to elements with an existing ID or class. 到目前为止,我只找到了为具有现有ID或类的元素分配其他类的方法。 Thanks, in advance, for your help!
在此先感谢您的帮助!
使用jQuery:
$(".post-categories a").addClass("btn");
Since the title says Using JavaScript 由于标题为“ 使用JavaScript”
Try this for a JavaScript solution: 试试这个JavaScript解决方案:
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)
{
if (links[i].parentNode.className == "post-categories")
{
links[i].className = "btn";
}
}
Or if you're down with the kids and you can use jQuery, you can do: 或者,如果您不喜欢孩子,并且可以使用jQuery,则可以执行以下操作:
$(".post-categories > a").addClass("btn");
use jquery 使用jquery
$(function() {
$(".post-categories a").addClass("btn");
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.