jquery - 无法更改背景图像:在 axios 之后 jquery 链接的 url
问题描述
这是我的代码。
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="el">
<input type="text" v-model="input.model"/>
<div v-for="item in items">
<a href="" class="image-wrapper background-image">
<img src="https://www.google.lk/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png" alt="">
</a>
</div>
</div>
<script src="http://themestarz.net/html/craigs/assets/js/jquery-3.3.1.min.js"></script>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: '#el',
delimiters: ["[[", "]]"],
data: {
input: {
sorting: "",
brand: "all",
model: "all",
},
items: null,
},
watch: {
input: {
handler(newInput) {
axios.get('http://api.fightpoverty.online/api/city?page=2')
.then(response => {
this.items = response.data;
console.log(JSON.parse(JSON.stringify(response.data)));
$(".background-image").each(function () {
$(this).css("background-image", "url(" + $(this).find("img").attr("src") + ")");
});
});
},
deep: true
}
},
created: function () {
axios.get('http://api.fightpoverty.online/api/city?page=2')
.then(response => {
this.items = response.data;
console.log(JSON.parse(JSON.stringify(response.data)));
$(".background-image").each(function () {
$(this).css("background-image", "url(" + $(this).find("img").attr("src") + ")");
});
});
}
});
</script>
</body>
</html>
我需要在 vue 的 created 事件中运行这个 jquery 代码。
$(".background-image").each(function () {
$(this).css("background-image", "url(" + $(this).find("img").attr("src") + ")");
});
但不幸的是,这并没有在 created event 中运行。但这在 watch event 中运行良好。所以代码可能是正确的,但我认为 created 事件可能会阻止代码运行。如果有人可以提供帮助,那就太好了。我需要的最终结果是这样的,
<a class="image-wrapper background-image" style="background-image: url('https://www.google.lk/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png');">
<img src="https://www.google.lk/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png" alt="">
</a>
解决方案
最后我设法使用 $ 修复它(document).ready()
。这是我的代码。保留这个以防有人发现它的用途。
axios.get('http://api.fightpoverty.online/api/city?page=2')
.then(response => {
this.items = response.data;
$(document).ready(function () {
$(".background-image").each(function () {
$(this).css("background-image", "url(" + $(this).find("img").attr("src") + ")");
});
});
});
推荐阅读
- hdfs - 列出具有两个特定字符串并位于 HDFS 中的所有文件目录
- java - 如何从 MySql PhpMyadmin 本地主机访问用户电子邮件和密码
- javascript - 我需要帮助在 Firefox 的开发控制台中创建循环
- autodesk-forge - 获取不同 id 的单个缩略图(子几何)
- oracle - 在处理过程中的最后一条语句之后,Oracle PL SQL 过程需要很长时间才能完成
- javascript - 无法捕获导致节点服务器崩溃的请求的套接字挂起错误
- android - JNI方法无法从类中获取包名称
- html - 使用赛普拉斯从 PrimeNG 中选择 p-dropdown 中的值
- javascript - 如何以最小的复杂性从javascript中的数组中获取不匹配的数组值?
- java - 存储在 MongoDB 中的 JSON 包含 \,\r,\n 字符