简体   繁体   English

如何将每个文本项包装在带有另一个标签的标签中,例如p

[英]How do I wrap each text item in a tag with another tag, like p

So, before you ask why, the idea is to find the position of the text on the screen, the only way I've found to do that is it's an actual element. 因此,在您问为什么之前,我们的想法是找到文本在屏幕上的位置,我发现这样做的唯一方法是它是一个实际元素。

$('button#orphan').click(function(){

    //The vars are defined globally for debugging.

    pNodeSplitText = new Array();
    pNodes=$('p');
    orphanedNodes=new Array();
    pNodes.each(function(index){
        pNodesHTMLbefore=pNodes.eq(index).html(); //May not be nessicary
        pNodeSplitText[index] = pNodes.eq(index).text().split(' '); //To get each text item
        pNodeSplitText.each(function(i){
          // this is where i will wrap each wrord, but how?
        })
    })
})

You didn't provide any HTML but if it's just like given (in the example here) HTML then you may try something like this ( Example ) 您没有提供任何HTML但是如果它只是给定的(在此处的示例中) HTML那么您可以尝试这样的事情( Example

HTML: HTML:

<p>Some text</p>
<p>Some more text</p>

JS: JS:

$('button#orphan').click(function(){
    var pNodeSplitText = [], orphanedNodes = [], pNodes = $('p');
    pNodes.each(function(i){
        pNodeSplitText = pNodes.eq(i).text().split(' ');
        var newText = '<span class="txt">' + pNodeSplitText.join('</span> <span class="txt">') + '</span>';
        $(pNodes[i]).html(newText);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM