当前位置: 动力学知识库 > 问答 > 编程问答 >

jQuery text split and join, does not wrap the first letter

问题描述:

I have a function that splits each letter from a span text and wrap it into an em. It works but the first letter from the span is not wrapped. How can I wrap that letter also in em?

 <script>

jQuery(document).ready(function(){

$(".nsplit").live('click', function(){

var newSplit = $("#selectable1 span.cica").text().split("").join("</em><em>");

$("#selectable1 span.cica").html(newSplit);

});

});

</script>

Thank you.

网友答案:

For the text "Test", the initial code:

$("#selectable1 span.cica").text().split("")

Would have produced an array ['T','e','s','t]

Calling join on the array inserts the separator between each array item, so you would end up with:

T</em><em>e</em><em>s</em><em>t

You then need to wrap the initial 'T' with a starting <em> and close off with a </em> on the final 't'

网友答案:

using window.parent you can get a reference to the parent of the iframe.

Is there anything that you want to achieve by nesting iframes? If yes, then you have to traverseup the tree using parent to get the topmost parent.

网友答案:

I've changed the code to something I found on jQuery examples and now it works. I can't figure it out why it did not work right the first time. Here is the new code if anyone have the same problem

  var words = $("#selectable1 span.cica").text().split("");
  var text = words.join("</em><em>");
  $("#selectable1 span.cica").html("<em>" + text + "</em>");
分享给朋友:
您可能感兴趣的文章:
随机阅读: