我很清楚span是一个内联元素,在大多数其他情况下,通过创建一个单独的span并在其上简单地使用display:block将是解决方案。我的代码有点复杂,因为我的代码与Javascript和半PHP代码纠缠在一起。所以这就是我要面对的:
模板文件(.tpl)中使用了这部分代码:
<p id="reduction_amount" {if !$product->specificPrice || $product->specificPrice.reduction_type != 'amount' || $product->specificPrice.reduction|floatval ==0} style="display:none"{/if}>
{strip}
<span id="reduction_amount_display">
{if $product->specificPrice && $product->specificPrice.reduction_type == 'amount' && $product->specificPrice.reduction|floatval !=0}
SAVE {convertPrice price=$productPriceWithoutReduction|floatval-$productPrice|floatval}
{/if}
</span>
{/strip}
</p>在.js文件中..And此部分:
if (combination.specific_price.reduction_type == 'amount') {
$('#reduction_amount_display').html('SAVE ' + formatCurrency(discountValue, currencyFormat, currencySign, currencyBlank));
$('#reduction_amount').show();
}它工作得很好,现在看起来是这样的:

(注意:<p>元素有固定的宽度和高度)
问题是,我想在"SAVE“部分之后换行,这样就可以用更大的字体来定义显示的数量。演示:

正如我在上面提到的,这可以通过将这两个跨度与另外两个跨度分开并将它们样式化成我想要的样子来实现。然而,由于我的Javascript知识相当有限,我不知道插入跨度的正确方法。我尝试了几种方法,例如在formatCurrency之前添加html += '<span class="blabla">',在括号后面添加结束标记,但由于某种原因,这有点搞乱了设计中的一切。我也尝试过不使用html +=,而直接使用引号,同样的事情也发生了。对此有什么解决方案?它不一定是Javascript代码。它可以是CSS或HTML解决方案。
编辑:
不幸的是,使用<br>是不可行的,因为我不能对单独显示的数量进行样式化。此外,使用该标记会产生一个巨大的差距,即数量远远超出(溢出) <p>元素。
发布于 2017-07-04 21:32:43
您可以使用append()将美元值包装在由jQuery创建的元素中,并使用给定的类对其进行相应的样式设置。
var amount = $('#reduction_amount_display');
amount.html('SAVE ');
amount.append('<div class="style-amount">$10</div>');.style-amount {
font-size:32px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="reduction_amount_display">
</p>
https://stackoverflow.com/questions/44907166
复制相似问题