简体   繁体   English

如何为字符串的第一个字符设置样式?

[英]How can I style the first character of a string?

I have no control over the HTML in this project, so everything has to be done with JavaScript/Jquery. 我无法控制此项目中的HTML,因此一切都必须使用JavaScript / Jquery完成。

I have multiple pages coming from a server. 我有来自服务器的多个页面。 Some of these tables have footers with footnotes. 其中一些表的页脚带有脚注。 The first character of these footnotes is either a * or a † 这些脚注的第一个字符是*或†

Sometimes the page will have multiple tables. 有时页面将有多个表。

I can't use ::first-letter, because it selects the symbol AND the first letter. 我不能使用:: first-letter,因为它会选择符号和第一个字母。 I need the padding to be only to the right of the symbol 我需要填充仅在符号的右边

I need to add padding to the right of these characters to separate them from the text. 我需要在这些字符的右侧添加填充以将其与文本分开。

I've successfully been able to get the symbols. 我已经成功地获得了符号。 As you can see in my code, I select the class, find the spans in the class, get the text, and convert them to sub-strings. 如您在我的代码中看到的,我选择了一个类,在该类中找到了跨度,获取了文本,并将其转换为子字符串。 I then get the first character of each one, and when I console.log my results, I see the symbols. 然后,我得到每个字符的第一个字符,当我console.log我的结果时,我看到了符号。 I'm now trying to wrap them in a span with some inline styling, but I'm getting a "jquery-2.1.4.min.js:3 Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': Only one element on document allowed." 我现在正尝试使用某种内联样式将它们包装在一个范围内,但是我得到了一个“ jquery-2.1.4.min.js:3 Uncaught DOMException:无法在'Node'上执行'insertBefore':仅一个元素允许。” error. 错误。

The HTML code is generated in xsl documents, but here's the table footer taken from the inspector: HTML代码是在xsl文档中生成的,但是这是从检查器中提取的表格页脚:

<div class="article-table-descriptions" style="width: 100%;">
    <span id="tblfn4">*footer text.</span>
    <span id="tblfn5">†footer text.</span>
</div>

$(document).ready(function () {

let chars;
let tableFooterSpans = $('.article-table-descriptions').find('span').text().split('.');

$(tableFooterSpans).each(function(i, str){
    chars = str.charAt(0);
    $(chars).wrap("<span style='color: lime;'></span>");
   })
});

try this one: 试试这个:

 $(document).ready(function () { var span = $('.article-table-descriptions > span'); span.each(function(index, el){ var span_html = $(el).html(); $(el).html("<span style='color: lime;'>"+ span_html.charAt(0) +"</span>" + span_html.substring(1,span_html.length)); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn4">*footer text.</span> <span id="tblfn5">†footer text.</span> </div> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn6">*footer text.</span> <span id="tblfn7">†footer text.</span> </div> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn8">*footer text.</span> <span id="tblfn9">†footer text.</span> </div> 

First get all the appropriate span elements before you try obtaining the first character. 在尝试获取第一个字符之前,首先获取所有适当的span元素。 Then loop through spans, getting the first character and getting the string minus first character, wrap the first character, then update span . 然后遍历跨度,获取第一个字符并获取字符串减去第一个字符,包装第一个字符,然后更新span

 $( () => { const $spans = $( '.article-table-descriptions > span' ); $spans.each( ( i, span ) => { const $span = $( span ); const str = $span.text(); const char = str.slice( 0, 1 ); const _str = str.slice( 1, str.length ); $span.html( `<span class="highlight--char">${char}</span>${_str}` ); } ); } ); 
 .highlight--char { margin-right: 0.5rem; color: lime; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn4">*footer text 1a.</span> <span id="tblfn5">†footer text 1b.</span> </div> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn4">*footer text 2a.</span> <span id="tblfn5">†footer text 2b.</span> </div> 

without jQuery you can do something like that 没有jQuery,您可以执行类似的操作

 let tableFooterSpans = document.querySelectorAll('.article-table-descriptions > span'); tableFooterSpans.forEach(e => e.innerHTML = e.innerHTML.replace(/(†|\\*)?/g,"<span style='color: lime;margin-right:5px'>$1</span>")); 
 <div class="article-table-descriptions"> <span>†text1</span> <span>*text2</span> </div> 

You can try something like this instead of usig wrap() 您可以尝试这样的事情,而不是使用usig wrap()

 $(document).ready(function () { let chars; let tableFooterSpans = $('.article-table-descriptions').find('span').text().split('.'); console.log("1",tableFooterSpans); $(tableFooterSpans).each(function(i, str){ chars = str.charAt(0); console.log("2", chars); $("#test").append("<span style='color : yellow'>"+chars+"</span>"); }) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <div class="article-table-descriptions" style="width: 100%;"> <span id="tblfn4">*footer text.</span> <span id="tblfn5">†footer text.</span> </div> <div id="test">test</div> 

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

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