在JavaScript中返回HTML代码? [英] Return HTML code in javascript?

查看:139
本文介绍了在JavaScript中返回HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,我想知道如何才能获得这段代码(更大整体的一部分)来做我想要的事情。我想在购物车中的'prhases'文章和'购物车中的文章'中添加HTML。这可能吗?非常感谢。

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.

我不是指造型(粗体或斜体),这是我想要它返回的:

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');
    }
},


推荐答案

当然这是可能的,尽管无论调用该函数是否对结果有意义的事情都是另一回事。

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

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

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

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

编辑:您添加到问题中的示例不起作用,因为它有语法错误 - 您的字符串文字包含换行符。使它成为一个有效的字符串,它可以通过将每个字符串全部放在一行或通过连接单个字符串来工作:

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>');

如果您使用的字符串之间的唯一区别是article是否有s结束然后试试这个:

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

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>';

这篇关于在JavaScript中返回HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆