gpt4 book ai didi

javascript - 如何使用javascript从div标签获取变量

转载 作者:行者123 更新时间:2023-11-30 14:06:20 24 4
gpt4 key购买 nike

我正在使用 javascript 遍历 div 并从 url 检索照片。循环运行完美,但我想从 div 标签中存储一个唯一的 id。

$(document).ready(function() {
var profileurl = 'https://testprofiles.com';
var serial = 'democompany';
$('.profile').each(function(i, obj) {
var uid = this.uid;
$("div").find('#avatar').attr('src', profileurl + '/' + serial + '/' + uid + '/api/avatar');
});
});
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>

<body>
<div class="profile" uid="206" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" uid="208" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>
</body>

</html>

我想从 <div> 中的 html 代码中获取 uid头。

最佳答案

使用 $(this).attr("uid");

$(document).ready(function() {
var profileurl = 'https://testprofiles.com';
var serial = 'democompany';
$('.profile').each(function(i, obj) {
let uid = $(this).attr("uid");
if(uid) {console.log(uid)}
$("div").find('#avatar').attr('src', profileurl + '/' + serial + '/' + uid + '/api/avatar');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile" uid="206" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" uid="208" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>

对自定义属性使用data-

$(document).ready(function() {
var profileurl = 'https://testprofiles.com';
var serial = 'democompany';
$('.profile').each(function(i, obj) {
let uid = $(this).data("uid");
if(uid) {console.log(uid)}
$("div").find('#avatar').attr('src', profileurl + '/' + serial + '/' + uid + '/api/avatar');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile" data-uid="206" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" data-uid="208" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>

您要求使用javascript 解决方案,但您使用的是jQuery 代码。坚持一个,不要混淆两者。下面是 javascript 解决方案。

$(document).ready(function() {
var profileurl = 'https://testprofiles.com';
var serial = 'democompany';
$('.profile').each(function(i, obj) {
let uid = this.dataset.uid; // or this.getAttribute('data-uid')
if(uid) {console.log(uid)}
$("div").find('#avatar').attr('src', profileurl + '/' + serial + '/' + uid + '/api/avatar');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile" data-uid="206" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" data-uid="208" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>
<div class="profile" style="float: left;">
<div><img id=avatar src=""></div>
</div>

关于javascript - 如何使用javascript从div标签获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com