使用JavaScript模板文本输出CSS中后台属性的值
我知道代码大体上是这样的,但我特别希望结合使用字符串作为值 所以它就像:使用JavaScript模板文本输出CSS中后台属性的值,javascript,jquery,template-literals,Javascript,Jquery,Template Literals,我知道代码大体上是这样的,但我特别希望结合使用字符串作为值 所以它就像: url(xxxxxx image xxxxxx) 提前谢谢你 更新 根据Hemant的建议尝试了以下内容 var bg=$("#hp-row1-1 > div.fp-bg").css('background-image').split('), url('); if(bg.length==2) { bg2 = `<div style="width:500px; height: 500px; backgroun
url(xxxxxx image xxxxxx)
提前谢谢你
更新
根据Hemant的建议尝试了以下内容
var bg=$("#hp-row1-1 > div.fp-bg").css('background-image').split('), url(');
if(bg.length==2) {
bg2 = `<div style="width:500px; height: 500px; background: url(${bg[1].replace(/\s+/g, '/')}"></div>`;
console.log(bg2);
bg[0] = bg[0].replace(/"([^"]+(?="))"/g, '$1');
console.log(bg[0])
$("#hp-row1-1 > div.fp-bg").css({
"background": `url(${bg[0]}) no-repeat center top !important;`});
$(bg2).insertAfter("#hp-row1-1");
};
<div style="background: url("http://www.davidnguyen.us/sinfulcolors.com/blog/wp-content/uploads/2019/02/HOME_Nudes_M_414x500.jpg")"></div>
将删除url(“path/to/image”)中的引号
但仍然得到:
<div style="width:500px; height: 500px; background: url("http://www.davidnguyen.us/sinfulcolors.com/blog/wp-content/uploads/2019/02/HOME_Nudes_M_414x500.jpg")"></div>
假设imgPath
是一个带有背景url的变量,您可以这样做
const bg = `<div style="background: url(${imgPath})"></div>`;
首先,您没有在模板中关闭url()
——您没有右括号。希望这是一个打字错误。如果你提供了一个真实的(最好是作为一个现场演示,在编辑器上有一个按钮)。不清楚数据来自何处。右大括号和右括号的顺序错误。@trincot嘿,谢谢,你能告诉我在哪里吗?在字符串的最后bg2=`
bg[0] = bg[0].replace(/"([^"]+(?="))"/g, '$1');
<div style="width:500px; height: 500px; background: url("http://www.davidnguyen.us/sinfulcolors.com/blog/wp-content/uploads/2019/02/HOME_Nudes_M_414x500.jpg")"></div>
const bg = `<div style="background: url(${imgPath})"></div>`;
$("#bg").css({
"background":`url( ${imgPath} )`
})