Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 如何在另一个dom元素中查找dom元素?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在另一个dom元素中查找dom元素?

Javascript 如何在另一个dom元素中查找dom元素?,javascript,jquery,Javascript,Jquery,我在ajax调用中返回了html: $.ajax({ url: '/get-html', dataType: 'html', success: function(html) { html = $(html); .... <div class="container"> <img src="" alt="PLEASE CHANGE THIS ALT"> </div> <img src="" a

我在ajax调用中返回了html:

$.ajax({
    url: '/get-html',
    dataType: 'html',
    success: function(html) {
        html = $(html);
        ....
<div class="container">
    <img src="" alt="PLEASE CHANGE THIS ALT">
</div>
<img src="" alt="LEAVE MY ALT ALONE">
<img src="" alt="LEAVE MY ALT ALONE">
在success方法中,我希望在html变量的container div中找到img标记,并更改它的alt标记

如果我这样做:

html.find('img').attr('alt', 'new alt');
我们更新所有图像上的alts。如何仅为container div指定alt?我试过这个:

html.find('.container img').attr('alt', 'new alt');
但它不起作用

仅供参考,以下是ajax调用中返回的HTML示例:

$.ajax({
    url: '/get-html',
    dataType: 'html',
    success: function(html) {
        html = $(html);
        ....
<div class="container">
    <img src="" alt="PLEASE CHANGE THIS ALT">
</div>
<img src="" alt="LEAVE MY ALT ALONE">
<img src="" alt="LEAVE MY ALT ALONE">

试试这个-

var html='';
//在更改alt attr之前打印html代码。
$('body').append(html);
$('body')。追加('hr/>');
html=$(html);
//检查内部img.div并更改属性
children('.container img').each(function(){
$(this.attr('alt','newalt');
//日志更改属性
log($(this.attr('alt'));
});
//更改alt attr后打印html代码。
$('body').append(html)

是100%确定,示例不起作用。
.container
是包装器,而不是
html
的子对象。所以使用
html.filter('.container')。find('img')
eisbehr这不起作用。哦,@A.Wolff是正确的,我错了…A。沃尔夫-你说得对,谢谢。