0

我希望用这样的方式替换空格:

<span>5</span>SPACE<image href="mtgsymbol.png" />

但是当我这样做时:

card_cost=card_cost.replace(/\s/g,"");

它把我的css搞得一团糟,对象相互堆叠,就像它们的位置变成了绝对一样。

如何在不破坏 CSS 或文档流的情况下替换空格?

示例代码:

Javascript:

card_cost=document.getElementById('card_cost').value;

if(card_cost)
{
card_cost=card_cost.replace(/\d+/g,"<span class='card_costnum'>$&</span>");
*breaks styling*card_cost=card_cost.replace(/\s/g,"");*breaks styling*
}

document.getElementById('output').innerHTML+="<div class='card'>"+"<span class='card_cost'>"+card_cost+"</span>";

CSS:

.card_name,.card_image,.card_cost,.card_type,.card_subtype,.card_rarity,.card_rules,.card_flavor,.card_strength,.card_num,.card_lower,.card_costnum
{
position:relative;
z-index:1;
}

.card_cost
{
display:inline-block;
float:right;
clear:right;
}

.card_costnum
{
position:relative;
display:inline-block;
text-align:center;
vertical-align:middle;
background-image:url("numsymbol_small.png");
background-repeat:no-repeat;
width:12px;
height:12px;
margin:1px;
top:-4px;
}
4

1 回答 1

3
card_cost = card_cost.replace(/\d+/g, "<span class='card_costnum'>$&</span>");
card_cost = card_cost.replace(/\s/g, "");

您正在将其变成<spanclass='card_costnum'>无效的,并且会破坏 HTML 的其余部分。只需反转替换即可。

card_cost = card_cost.replace(/\s/g, "");
card_cost = card_cost.replace(/\d+/g, "<span class='card_costnum'>$&</span>");
于 2012-04-22T17:00:33.487 回答