English 中文(简体)
Wrap text after particular symbol with jQuery
原标题:
  • 时间:2009-11-09 14:55:31
  •  标签:
  • jquery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself.

This is my html:

<ul>
    <li>Some list text - Additional text</li>
</ul>

As I result I m trying to achieve this:

<ul>
     <li>Some list text <span class="after">- Additional text<span></li>
</ul>

Will anyone will be able to help me please?

Thank you in advance.

最佳回答

Something like this should do it.

$( li ).each(function() {
  $(this).html($(this).text().replace(/-.*$/,  <span class="after">$&</span> ));
});
问题回答

It looks like you are trying to create some sort of descriptive list instead of a simple unordered list. If you need to keep all the text within a single element I m sure someone will post a decent solution, but why not use a dl instead of a ul like so:

 <dl>
      <dt>Some list text</dt>
      <dd>- additional text</dd>
 <dl>

This should achieve the same effect as trying to cause the text after a "-" sign to appear on a new line, and you can modify the padding and margin on the <dt> and <dl> elements so that they line up if that is the look you are going for. If you are creating a descriptive list with list items (<dt>) with further sub list items describing them (<dd>) its better to imply that in the semantics for accessibility than to approximate the same thing using another method.

EDIT: Before anyone says anything I am aware that the question asked for a solution using jQuery, and that this solution doesn t use jQuery. But if the intent here is to approximate a descriptive list it s better to code that semantically in the HTML than use jQuery magic.

This will insert a linebreak before every - in a list item.

$(function(){
  $("li").each(function(){
    $(this).html($(this).html().replace(-, "<br />-"));
  });
});

Sounds like you re on track. Make the span a block-level element like so:

<span class= after  style= display: block; >...</span>

And see if that does it.

Oh, and then move that inline style into your stylesheet :)

Something like this below should do it, note this was dry-coded here in my browser, so testing is required.

$( ul li ).each(function() {
    var parts = $(this).html().split( - , 1);

    $(this).html(parts[0]);
    if (parts.length == 2) {
        $(this).append(" - " + parts[1]);
    }    
});
$.each($("li:contains( - )"),function(i,n){
  n.innerHTML = n.innerHTML.replace( - , <span class="after">- )+"</span>"
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签