Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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读取参数_Javascript_Css - Fatal编程技术网

通过普通Javascript读取参数

通过普通Javascript读取参数,javascript,css,Javascript,Css,下面是一些建议-我将一个带有参数的onclick事件传递到JS中,如下所示: <div class="artistthumb" id="jamesPaterson1" onclick="artistModalOpen('jamesPaterson1')"></div> id的CSS为: background-image: url(../img/james_paterson_1.jpg); 到目前为止,我只记录了2个控制台输出-第一个很好,控制台按预期显示参数x

下面是一些建议-我将一个带有参数的onclick事件传递到JS中,如下所示:

<div class="artistthumb" id="jamesPaterson1" onclick="artistModalOpen('jamesPaterson1')"></div>    
id的CSS为:

background-image: url(../img/james_paterson_1.jpg);
到目前为止,我只记录了2个控制台输出-第一个很好,控制台按预期显示参数x1:
jamesPaterson1

第二个控制台日志没有显示任何内容

我希望看到
url(../img/james\u paterson\u 1.jpg)

有人知道为什么这不会发生吗


非常感谢

试试
getComputedStyle

var imagePath = getComputedStyle(document.getElementById(x1))
                .getPropertyValue('background-image');

window.getComputedStyle()
方法返回报告的对象 应用active Directory后元素的所有CSS属性的值 样式表和解析这些值可能需要的任何基本计算 包含。单个CSS属性值通过API访问 由对象提供,或通过简单地使用CSS属性名进行索引来提供


style
对象不显示来自样式表的CSS规则,@Pointy就是这样!非常感谢。您可以使用getComputedStyle来达到您的目的
var imagePath = getComputedStyle(document.getElementById(x1))
                .getPropertyValue('background-image');