我认为解决这个问题的最好方法是直接跳进去,边走边解释。
我使用的wordpess与自定义模板(我们做它)这段代码是用来处理twitter的小工具亲。(这不是最漂亮的代码,我稍后会清理它)
<?php
/*
* TWITTER SIDE BAR
*
* Sets the twitter area to display: none and pulls in the content from the server
* Javascript then does a string replace to remove a bit of unwanted text
* finally javascript will write the doctored string to the client browser
*/
?><div id="twitterRight">
<ul class="xoxo" style="list-style-type:none; font-size:11px; margin:0px 20px 0 0;">
<li id="twitter-2" class="widget-container widget_twitter">
<div id="twitHold" style="display:none;">
<h3 class="widget-title"><span class='twitterwidget twitterwidget-title'><a href="http://www.twitter.com/username" target="_blank"><img src="<?php echo home_url(); ?>/images/twitterName.png" width="208" height="27" alt="EhomeS" /></a></span></h3>
<ul>
<?php
$twitter = dynamic_sidebar('primary-widget-area');
?></ul>
</div>
<div id="twitHolder">
<script type="text/javascript">
// Get the posted content from the server
var str = $('#twitHold').html();
var x = str.replace("Twitter: @username", "");
//var shortString = x.substr( 0, 10 );
document.write(x);
</script>
</div>
</li></ul>
</div><!-- END #twitterRight -->问题是wordpress函数dynamic_sidebar不返回字符串或任何东西,只返回一个布尔值,所以我不能操作它。所以我所做的就是将输出的HTML存储在一个js变量x中,并从那里对其进行操作。
我试图实现的是简单地限制每个列表项(Tweet)中的字符数,但是我找不到这样做的方法。到目前为止,我已经尝试过this,但没有成功(我想是因为javascript正在写出并解析它,我不确定)。
有没有办法对列表项执行substr?
发布于 2012-08-28 17:33:17
这个问题有点不清楚,如果你想要一个列表项的集合,那么试试这个。
使用jquery选择器,jquery有很好的文档http://jquery.com/
确保将jquery库添加到您的文档中。
<ul id="twitterlist">
<?php
$twitter = dynamic_sidebar('primary-widget-area');
?></ul>
<script type="text/javascript">
var listitems = $('#twitterlist').children();
</script>https://stackoverflow.com/questions/12156199
复制相似问题