如何用Jquery中的某个单词包装一个句子

时间:2015-06-24 03:35:08

标签: jquery contains

如何在jquery中执行此操作?说我有

<div class = "container">
      The requested quantity for "ITEM" is not available

      <span>Other word</span>
</div>

如果容器有这个短语“请求的数量” 然后将整个句子包装在

<h1></h1>

这将是输出:

<div class = "container">
      <h1>The requested quantity for "ITEM" is not available</h1>

      <span>Other word</span>
</div>

谢谢:)

3 个答案:

答案 0 :(得分:1)

你可以试试这个: -

$('.container').html('<h2>'+$('.container').text()+'</h2>');

或者您也可以使用wrapInner: -

$('.container').wrapInner('<h2/>');

回答更新的问题: -

$('.container').contents()
    .filter(function(){return this.nodeType === 3})
    .wrap('<h2 />');

Demo for wrapInner

Demo

答案 1 :(得分:0)

$("#container").html('<h1>'+$("#container").html()+'</h1>');

答案 2 :(得分:0)

$('div.container').each(function(index){
    if($(this).html().indexOf("The requested quantity for") > -1)
    {
        $(this).html("<h1>" + $(this).html() + "</h1>");
    }
});
相关问题