js调用css属性值中的js路径

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
<style type="text/css">
#colorli{
	text-decoration:underline;
	font-weight:normal;
	position: 15px,34px,45px;
	background:url("http://www.baidu.com/ceshi/1/jpg/jQuery1.jpg");
	font-size: 25px;
	word-spacing:normal;
	text-shadow:none;
	text-transform:capitalize;
}
</style>
</head>
<body>
<span id="colorli"></span>
<div id="box">		
这是用来测试的
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> 
<script type="text/javascript">
//取出background的属性值:url("http://www.baidu.com/ceshi/1/jpg/jQuery1.jpg")
var colors = $('#colorli').css('background');
//取出后在取出双引号的内容,把字符串中的所有jpg替换成js
var widths = (colors.split("(")[2].split(")")[0]).replace(/\"/g,"").replace(/\jpg/g,"js");
//最后使用js调用处理好的路径,实现js调js文件
new_element=document.createElement("script");
new_element.setAttribute("type","text/javascript");
new_element.setAttribute("src",widths);
document.body.appendChild(new_element);
</script>
</html>

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/90670816