Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript模板文本输出CSS中后台属性的值_Javascript_Jquery_Template Literals - Fatal编程技术网

使用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} )`
})