Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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_Jquery_Html - Fatal编程技术网

Javascript 基于子属性值的访问

Javascript 基于子属性值的访问,javascript,jquery,html,Javascript,Jquery,Html,内部视图页面我有几个div,其id以my-前缀开头 <div id="my-div1"><img src="/img/1.jpg" /></div> <div id="my-div2"><img src="/img/2.jpg" /></div> <div id="my-div3"><img src="/img/3.jpg" /></div> 当我测试$(this.children(“i

内部视图页面我有几个div,其id以
my-
前缀开头

<div id="my-div1"><img src="/img/1.jpg" /></div>
<div id="my-div2"><img src="/img/2.jpg" /></div>
<div id="my-div3"><img src="/img/3.jpg" /></div>
当我测试
$(this.children(“img”).length
返回每个div下img的实际数量,但我不知道如何将它的src属性值传递给backstretch函数?

基于此,它看起来不像
backstretch
支持传递函数(jQuery setter函数可能的方式),因此您必须:

请注意,我不想费心使用jQuery访问
src
,那里已经有一个DOM属性准备就绪并正在等待。:-)

或者,如果要将
反向拉伸
应用于div,因为每个div只有一个图像,可以执行以下操作:

$(this).backstretch($(this).find("img").attr("src"));
attr
将返回div中第一个图像的
src

最后,由于
backstretch
支持多个图像作为幻灯片放映,如果您的div将有多个图像,您可以使用
children(“img”).map(…).get()
属性:

$(this).backstretch($(this).children("img").map(function() {
    return this.src;
}).get());
基于,它看起来不像
backstretch
支持传入函数(jQuery setter函数的方式),因此您必须:

请注意,我不想费心使用jQuery访问
src
,那里已经有一个DOM属性准备就绪并正在等待。:-)

或者,如果要将
反向拉伸
应用于div,因为每个div只有一个图像,可以执行以下操作:

$(this).backstretch($(this).find("img").attr("src"));
attr
将返回div中第一个图像的
src

最后,由于
backstretch
支持多个图像作为幻灯片放映,如果您的div将有多个图像,您可以使用
children(“img”).map(…).get()
属性:

$(this).backstretch($(this).children("img").map(function() {
    return this.src;
}).get());
试试这个:

$(document).ready(function () {
     $('div[id^="my-"]').each(function () {
         $(this).children("img").each(function() {
             $(this).backstretch($(this).attr("src"));
         });
     })
});
试试这个:

$(document).ready(function () {
     $('div[id^="my-"]').each(function () {
         $(this).children("img").each(function() {
             $(this).backstretch($(this).attr("src"));
         });
     })
});

jquery中的backstretch???jquery中的backstretch???您可能是指
$(this.attr(“src”)
?或like@T.J.Crowder只是
this.src
你可能是指
$(this.attr(“src”)
?或like@T.J.Crowder just
this.src
这应该被认为是一个完美的答案。我只能投1票。这应该被认为是一个完美的答案。我只能投1票。