Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
Jquery 获得;alt";来自驻留在<;中的图像的属性;a>;标签_Jquery_Next - Fatal编程技术网

Jquery 获得;alt";来自驻留在<;中的图像的属性;a>;标签

Jquery 获得;alt";来自驻留在<;中的图像的属性;a>;标签,jquery,next,Jquery,Next,这应该相当简单,但由于某些原因,它不起作用 我想从位于标记内部的图像中获取“alt”属性 <div id="album-artwork"> <ul> <li><a href="link to large image"><img src="thumbnail" alt="description of the image" /></a></li> ... </ul> </div>

这应该相当简单,但由于某些原因,它不起作用

我想从位于标记内部的图像中获取“alt”属性

<div id="album-artwork">
<ul>
   <li><a href="link to large image"><img src="thumbnail" alt="description of the image" /></a></li>
   ...
</ul>
</div>



$("#album-artwork a").click(function(e) {
e.preventDefault();

var src = $(this).attr("href");
var alt = $(this).next("img").attr("alt");


alert(src); // ok!
alert(alt); //output: undefined

});

  • ...
$(“#相册插图a”)。单击(功能(e){ e、 预防默认值(); var src=$(this.attr(“href”); var alt=$(this.next(“img”).attr(“alt”); 警报(src);//好的! 警报(alt);//输出:未定义 });
知道为什么next()不起作用吗?有更好的解决办法吗

提前谢谢
Marco

这是因为图像标签在ul标签内。接下来只查找相同级别的标记。你必须使用

var alt = $(this).children("img").attr("alt");

编辑:包括缺少的附加“信息”(this.find('img'))而是选择链接中的某个元素。

不会。下一步选择下一个元素,即下一个Li而不是IMG?

无所谓。我刚刚得到它……我意识到
IMG
不是
的兄弟,而是
的孩子

这是正确的语法

var alt = $(this).children("img").attr("alt"); 


测试:

尝试此测试及其工作

<div id="album-artwork">
<ul>
   <li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
</ul>
</div>
children()
函数可查找任何子项,但如果要查找任何特定的子项,请像这样定义子项名称
children(“img”)
子项(“按钮”)
不要复杂化:

$('img').on({'click': function(){
    var alt = this.alt;         

我刚刚回答了我自己的问题…忘记了兄弟姐妹和孩子们…你的答案有助于确认我的答案。谢谢!jQuery将
$(孩子,家长)
内部重写为
$(家长)。查找(孩子)
。检查我的@user27841答案是否经过测试并正常工作!!!Marco检查我的@user27841答案。。。
<div id="album-artwork">
<ul>
   <li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
</ul>
</div>
    $(document).ready(function() {
        $("#album-artwork a").click(function(e) {
            e.preventDefault();

            var src = $(this).attr("href");
            //var alt = $(this).next("img").attr("alt");
            var alt = $(this).children().attr("alt");
            //find function also do the same thing if you want to use.
            /*var alt = $(this).find("img").attr("alt");*/

            alert(src); // ok!
            console.log(src); // ok!
            alert(alt); //output: not undefined now its ok!
            console.log(alt); //output: not undefined now its ok!
        });
    });
$('img').on({'click': function(){
    var alt = this.alt;