将HTML添加到Javascript IF函数
我正在尝试编辑一个脚本,以便可以使用HTML格式化输出,但不确定如何正确格式化它。目前的脚本如下,但我想实现的是根据下面的第二个脚本。我知道第二个脚本没有意义,但我只是想展示我基本上想要实现的,但不确定如何实现它。如果有人能帮忙,我将不胜感激 原始脚本:将HTML添加到Javascript IF函数,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试编辑一个脚本,以便可以使用HTML格式化输出,但不确定如何正确格式化它。目前的脚本如下,但我想实现的是根据下面的第二个脚本。我知道第二个脚本没有意义,但我只是想展示我基本上想要实现的,但不确定如何实现它。如果有人能帮忙,我将不胜感激 原始脚本: var selectCallback = function(variant, selector) { timber.productPage({ money_format: "{{ shop.money_format }}", varia
var selectCallback = function(variant, selector) {
timber.productPage({
money_format: "{{ shop.money_format }}",
variant: variant,
selector: selector
});
if (variant) {
if (variant.inventory_management == "shopify" && variant.inventory_policy != "continue") {
if (variant.inventory_quantity > 0) {
jQuery('#variant-inventory').text('We have ' + variant.inventory_quantity + ' in stock.');
} else {
jQuery('#variant-inventory').text("This product is sold out");
}
} else {
jQuery('#variant-inventory').text("In stock - order now");
}
} else {
jQuery('#variant-inventory').text("");
}
};
我正在努力实现的目标:
var selectCallback = function(variant, selector) {
timber.productPage({
money_format: "{{ shop.money_format }}",
variant: variant,
selector: selector
});
if (variant) {
if (variant.inventory_management == "shopify" && variant.inventory_policy != "continue") {
if (variant.inventory_quantity > 0) {
jQuery('#variant-inventory').text('<div class="stock">We have ' + variant.inventory_quantity + ' in stock.</div>');
} else {
jQuery('#variant-inventory').text("<div class="sold-out">This product is sold out</div> - <a href="link">contact us</a>");
}
} else {
jQuery('#variant-inventory').text("<div class="in-stock">In stock</div> - order now");
}
} else {
jQuery('#variant-inventory').text("");
}
};
var selectCallback=函数(变量,选择器){
木材.产品页({
money_格式:“{{shop.money_格式}}”,
变体:变体,
选择器:选择器
});
if(变体){
如果(variant.inventory\u management==“shopify”&&variant.inventory\u policy!=“continue”){
如果(变量库存\数量>0){
jQuery(“#variant inventory”).text('wehave'+variant.inventory_quantity+'in stock');
}否则{
jQuery(“#variant inventory”).text(“此产品已售罄-”;
}
}否则{
jQuery(“#variant inventory”).text(“现在有库存订单”);
}
}否则{
jQuery(“#变量清单”).text(“”);
}
};
而不是使用.text()
使用
应该是:
'<div class="sold-out">This product is sold out</div> - <a href="link">contact us</a>'
不要使用
.text()
使用
应该是:
'<div class="sold-out">This product is sold out</div> - <a href="link">contact us</a>'
使用jQuery.html()而不是jQuery.text()使用jQuery.html()而不是jQuery.text()请注意突出显示的语法以及它如何开始打断您试图在字符串中使用的引号。您需要在字符串中转义引号。请注意突出显示的语法以及它是如何开始打断您试图在字符串中使用的引号的。您需要在字符串中转义引号。非常感谢Roko。这样的事情一经解释就很简单了,但对我来说,一开始就试着记住它们是一个很大的学习过程!谢谢你提供的所有信息,我很感激。非常感谢Roko。这样的事情一经解释就很简单了,但对我来说,一开始就试着记住它们是一个很大的学习过程!谢谢你提供的所有信息,我很感激。
'<div class="sold-out">This product is sold out</div> - <a href="link">contact us</a>'
jQuery(function( $ ) { // DOM ready and $ alias secured in scope
// Your DOM ready code here.
// Now you're free to use the $ alias and save some bites and finger nails
$('#variant-inventory').html("YEY!!");
});