Html 在javascript中返回HTML代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19845296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 15:08:42  来源:igfitidea点击:

Return HTML code in javascript?

javascripthtmlreturn

提问by Owen Parsons

I'm fairly new to javascript and I'm wondering how I could get this piece of code (part of a bigger whole) to do what I want. I'd like to add HTML to the prhases 'articles in shopping cart' and 'article in shopping cart'. Is this possible? Many thanks.

我对javascript相当陌生,我想知道如何让这段代码(更大整体的一部分)做我想做的事。我想在短语“购物车中的文章”和“购物车中的文章”中添加 HTML。这可能吗?非常感谢。

I don't mean styling (bold or italic), this is what i'd want it to return:

我的意思不是样式(粗体或斜体),这就是我希望它返回的内容:

return quantity + (quantity == 1 ? ' 
  article in shopping cart <span class="simpleCart_quantity"></span>- 
 <span class="simpleCart_total"></span>
 <a href="shoppingcart.html">Show</a>' : 
 ' articles in shopping  cart 
  <span class="simpleCart_quantity"></span>-
  <span class="simpleCart_total"></span>
  <a href="shoppingcart.html">Show</a>');

I know this is not possible, how is it possible?

我知道这是不可能的,怎么可能?

quantity: function () {
    var quantity = 0;
    simpleCart.each(function (item) {
        quantity += item.quantity();
    });
    if (quantity == 0) {
        return 'Your shopping cart is empty';
    } else {
        return quantity + (quantity == 1 ? ' article in shopping cart' : ' articles in shopping cart');
    }
},

采纳答案by nnnnnn

Of course it's possible, although whether whatever calls that function does something sensible with the result is another matter.

当然这是可能的,尽管调用该函数的任何内容是否对结果进行了明智的处理是另一回事。

Anyway, just include the desired html in the strings as required:

无论如何,只需根据需要在字符串中包含所需的 html:

return quantity + (quantity == 1 ? ' article <span class="x">in shopping cart</span>' :
                                   ' articles <i>in</i> shopping cart');

EDIT: the example that you added to the question doesn't work because it has syntax errors - your string literal contains newlines. Make it a valid string and it will work, either by putting each string all on one line or by concatenating individual strings:

编辑:您添加到问题中的示例不起作用,因为它有语法错误 - 您的字符串文字包含换行符。使它成为一个有效的字符串,它会起作用,要么将每个字符串都放在一行上,要么连接单个字符串:

return quantity + (quantity == 1 ?  
  'article in shopping cart <span class="simpleCart_quantity"></span>- <span class="simpleCart_total"></span> <a href="shoppingcart.html">Show</a>' : 
  'articles in shopping  cart <span class="simpleCart_quantity"></span>- <span class="simpleCart_total"></span> <a href="shoppingcart.html">Show</a>');

If the only difference between the strings you are using is whether "article" has an "s" on the end then try this:

如果您使用的字符串之间的唯一区别是“文章”末尾是否有“s”,请尝试以下操作:

return quantity + (quantity == 1 ? 'article' : 'articles') +
       ' in shopping cart <span class="simpleCart_quantity"></span>- <span class="simpleCart_total"></span> <a href="shoppingcart.html">Show</a>';