简体   繁体   English

如何在javascript中从标签获取文本,修剪并再次粘贴到标签

[英]How to get text from tags, trimm, and paste to tags again in javascript

I would like to trimm text from html tags, and paste result to these tags again. 我想修剪html标签中的文本,然后再次将结果粘贴到这些标签中。 It's not DOM content, only string. 它不是DOM内容,只有字符串。

var string = "<div class='someClass'><b>Some very long text</b></div>"

Wanted result is fe: 想要的结果是fe:

var string = "<div class='someClass'><b>Some very lon</b></div>"

I found library striptags , but it only gets rid off tags, but I want to keep them. 我找到了库剥离标签 ,但是它只能摆脱标签,但是我想保留它们。

If you have any solution please let me know :) 如果您有任何解决方案,请告诉我:)

UPDATE: 更新:

Thanks all of you for advices. 谢谢大家的建议。 There are few things to add from me: 1. I never have information about html tags, because it came from quill text editor, and I need some kind of regex. 我需要添加的内容很少:1.我从来没有关于html标签的信息,因为它来自羽毛笔文本编辑器,并且我需要某种正则表达式。 2. In my job there is no jQuery, it's kind of 'evil' :P. 2.在我的工作中没有jQuery,有点“邪恶”:P。 3. I'm using react, so any use of 'document' or 'window' is unwanted here :(. 3.我正在使用react,所以这里不需要使用'document'或'window':(。

Checkout trim() method of javascript or String.prototype.trim() . javascript或String.prototype.trim()的 Checkout trim()方法。

Hope it will work for you! 希望它对您有用!

If it is a string and you want to trim some portion of text. 如果它是字符串,并且要修剪文本的某些部分。 you can use the substring/slice function of javascript. 您可以使用javascript的substring / slice函数。

For more refernce you can refer below links 有关更多参考,您可以参考以下链接

http://www.w3schools.com/jsref/jsref_substring.asp http://www.w3schools.com/jsref/jsref_substring.asp

http://www.w3schools.com/jsref/jsref_slice_string.asp http://www.w3schools.com/jsref/jsref_slice_string.asp

If the structure is exact like you showed <div class='someClass'><b>Some very long text</b></div> , you could do it with regex to find the text, and in the function change it how you like, shorten( substr )/ trim /...: 如果结构与显示<div class='someClass'><b>Some very long text</b></div>一样精确,则可以使用正则表达式来查找文本,然后在函数中更改它的方式你喜欢,short( substr )/ trim / ...:

 var longText ="<div class='someClass'><b>Some very long text</b></div>"; const MAX_LENGTH = 13; // regex expression matches the structure given var shortText = longText.replace(/(<div[^>]+><b>)([^<]+)(?=<\\/b><\\/div>)/gi, function(m1,m2,m3){ // m2 matches "<div class='someClass'><b>" // m3 matches "Some very long" return m2 + m3.substr(0, MAX_LENGTH).trim(); }) console.info(shortText) 

Here some documentation to the replace function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 这里是替换功能的一些文档https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

A "cooler" Alternative: 一个“更酷”的选择:
If you pass the HTML Element that should be altered, to this short function checkChildren , all Child-Textnodes will be modified, trim , substr , ... (it is a bit of an overkill, checking every node, if you know the final structure, but i just wanted to test it): 如果您将应更改的HTML元素传递给此简短函数checkChildren ,则将修改所有Child-Textnodes, trimsubstr ,...(如果您知道最后一个,检查每个节点都有些过头结构,但我只想测试一下):

 var elem = document.getElementById("test"); //define the max length of text nodes const MAX_LENGTH = 13; checkChildren(elem); // check all children from parent element an limit the size of textNodes function checkChildren(parent){ for(var i=0; i<parent.childNodes.length;i++){ var child = parent.childNodes[i]; switch(child.nodeType){ case Node.ELEMENT_NODE: // recursive call checkChildren(child); break; case Node.TEXT_NODE: // modify textNode (Shorten and trim) child.nodeValue = child.nodeValue.substr(0,MAX_LENGTH).trim(); break; } } } 
 <div id="test" class='someClass'><b>Some very long text</b></div> 

You could use a hidden tag where you manipulate the string. 您可以在操作字符串的地方使用隐藏标签。 Something like <div id="tags-modifier" ></div> and then jquery like like <div id="tags-modifier" ></div> ,然后是jquery之类的东西

var string = "<div class='someClass'><b>Some very long text</b><b>Test2213213213213</b></div>"
$("#tags-modifier").html(string);
var part= 5;
$.each($('#tags-modifier *:not(:has("*"))'), function(){
    mytext=$(this).html()
    $(this).html(mytext.substring(0, part))
})
string = $("#tags-modifier").html();

This works even if there are multiple siblings 即使有多个兄弟姐妹也可以使用

Fiddle here 在这里摆弄

You can use $.parseHTML to convert this string into dom tree. 您可以使用$ .parseHTML将此字符串转换为dom树。 Get the <b> node from this dom tree and change the text with the help of substr. 从此dom树中获取<b>节点,并在substr的帮助下更改文本。

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

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