简体   繁体   English

删除跨度之间的空白

[英]Remove a white space between spans

I have spans setup as so.. 我有跨度设置..

<span class='Main'>
   <span id='id1'>some</span> <span id='id2'>words</span>
</span> 
<span>.</span>

This outputs 这个输出

some words . 一些单词 。

Is there a way to remove the space before the period & keep the rest of the spaces intact? 有没有一种方法可以在句点之前删除空间并保持其余空间不变?

I've tried trim() but that removes all spaces in my string; 我已经尝试过trim()但这会删除字符串中的所有空格; there are many words preceding (each within a span) that i do not want effected. 我不想影响前面的很多单词(每个单词都在一个范围内)。

Is there a way to 'target' that particular space? 有没有一种方法可以“瞄准”特定的空间?

Ed: I can edit the HTML & remove the white space, but i want to do it programmatically. 埃德:我可以编辑HTML并删除空白,但我想以编程方式进行。

Since <span> elements are inline elements by default, the white space between them will be respected (displayed). 由于<span>元素默认为inline元素,因此将尊重(显示)它们之间的空白。 One way to avoid this is to remove the undesired white space between the elements in your HTML code: 避免这种情况的一种方法是删除HTML代码中元素之间不需要的空格:

 <span class='Main'><span id='id1'>some</span> <span id='id2'>words</span></span><span>.</span> 


Edit 编辑

If you don't want to alter the HTML code, maybe you can use CSS to simulate spaces between words. 如果您不想更改HTML代码,则可以使用CSS来模拟单词之间的空格。

In the example below, I'm setting the font-size for the container to zero to hide white space, then setting the font-size of <span> elements to a readable size (a method posted by thirtydot and by dsfq ). 在下面的示例中,我将容器的font-size设置为零以隐藏空白,然后将<span>元素的font-size设置为可读的大小( 三十点dsfq发布的方法)。 Then I'm adding some margin before each <span> that is not the first or last child. 然后,我在不是第一个或最后一个孩子的每个<span>之前添加一些边距。

Admittedly, this method is not very reliable, especially if you can't predict whether there will be a period or not. 诚然,这种方法不是很可靠,尤其是在您无法预测是否会有周期的情况下。

 .container { font-size: 0; } .container span { font-size: 16px; background: #DDD; } .container span:not(:first-child):not(:last-child) { margin: 0 0 0 .5em; } 
 <div class="container"> <span>some</span> <span>words</span> <span>go</span> <span>here</span> <span>.</span> </div> 

Although not neat I decided to code this with some jquery and js: 虽然不整洁,但我还是决定使用一些jquery和js进行编码:

var get = "";
var array = [];
var i = 0;
var concat = "";
var str;

function doAction() {
    $("span").each(function (index, value) {

        if (index != 0) {

            get = $(this).text() + " ";
            array[index] = get;
            if (array[index].trim() == '.') {

                array[index] = array[index - 1].trim() + array[index];
                array[index - 1] = "";


            }
        }
    });
    $("span").each(function (index, value) {

        if (index != 0) {
            $(this).text(array[index]);
        }

    });
}
doAction();

and here is the fiddle. 这是小提琴 I added more spans to it and it works again. 我添加了更多的跨度,它又可以正常工作了。 Hope it is good, url: https://jsfiddle.net/eugensunic/kphe5fbL/4/ 希望它很好,URL: https : //jsfiddle.net/eugensunic/kphe5fbL/4/

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

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