最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parts of string in bold text - Stack Overflow

programmeradmin1浏览0评论

I have a unordered list that contains

<ul id="strip">
     <li><a href="#"><span>This-is a test string</span></a></li>
    <li><a href="#"><span>This is without</span></a></li>
     <li><a href="#"><span>New-test</span></a></li>
</ul>

I need to bold the text before the "-", so in the first <li> "This" is bolded.

I'm stuck in the loop where I should find the "-".

NB: Regular JavaScript, no JQuery :-)

I have a unordered list that contains

<ul id="strip">
     <li><a href="#"><span>This-is a test string</span></a></li>
    <li><a href="#"><span>This is without</span></a></li>
     <li><a href="#"><span>New-test</span></a></li>
</ul>

I need to bold the text before the "-", so in the first <li> "This" is bolded.

I'm stuck in the loop where I should find the "-".

NB: Regular JavaScript, no JQuery :-)

Share Improve this question asked Aug 13, 2009 at 6:55 janhartmannjanhartmann 15k17 gold badges87 silver badges140 bronze badges 3
  • 1 i'm simply curious: why no jQuery? – Scott Evernden Commented Aug 13, 2009 at 6:59
  • Is the down vote for 'no jQuery'? – rahul Commented Aug 13, 2009 at 7:02
  • My bad, didn't read the "no jQuery" part. Sorry! – Gabriel Hurley Commented Aug 13, 2009 at 7:20
Add a ment  | 

1 Answer 1

Reset to default 5

Use stringObject.replace(findstring,newstring) method.
In your case liString.replace(/\b(.*?-.*?)\b/,"<strong>$1</strong>")
full solution:

var lists = document.getElementsByTagName("li");
var listsL = lists.length;

for (var i = 0; i < listsL; ++i){
   liString = lists[i].innerHTML;
   liString = liString.replace(/\b([^-]*-[^\b]*?)\b/,"<strong>$1</strong>");
   lists[i].innerHTML = liString;
}
发布评论

评论列表(0)

  1. 暂无评论