插入< span> in< p>元件 [英] Insert <span> in <p> element

查看:169
本文介绍了插入< span> in< p>元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢;

 < p>变量文本< / p> 

我想要它;

 < p>变量< span>文字< / span>< / p> 

这是可以通过javascript函数吗? /或jQuery。

哦,是的,p元素有一个ID,p元素中的文本是可变的,但总是由2个单词组成。我想通过JavaScript函数跨越文本的最后一个词。

解决方案

试试这个

  var txt =Hello bye; 
var dataArr = txt.split('');

var paragraph = document.getElementById(pid);
paragraph.innerHTML = dataArr [0] +< span>+ dataArr [1] +< / span>;

这是演示


I got like;

<p>Variable Text</p>

And I want to it to be;

<p>Variable <span>Text</span></p>

Is this possible by a javascript function? / or jQuery.

Oh yeah, and the p-element got an ID and the text inside the p-element is variable but always consists of 2 words. I want a span around the last word of the text by a javascript function.

解决方案

Try this

var txt = "Hello bye";
var dataArr = txt.split(' ');

var paragraph = document.getElementById("pid");
paragraph.innerHTML = dataArr[0]+ " <span>"+dataArr[1]+"</span>";

Here is a demo

这篇关于插入&lt; span&gt; in&lt; p&gt;元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆