简体   繁体   English

如何从这个大的div元素中获取图像元素?

[英]How to get image element from this large div element?

So I have a template of a block of code using json2html that is 所以我有一个使用json2html的代码块模板

var template = {'<>':'div','class':'col-lg-12 col-md-24 col-sm-24 col-xs-24','html':[
    {'<>':'div','class':'product col-lg-offset-1 col-lg-22 col-md-offset-0 col-md-24 col-sm-offset-0 col-sm-24 col-xs-offset-0 col-xs-24','html':[
        {'<>':'div','class':'prodimg col-lg-8 col-md-8 col-sm-8 col-xs-8','html':[
            {'<>':'img','src':'${source}','alt':'${name}', 'class':'img-responsive'}
        ]},
        {'<>':'div','class':'prodetails col-lg-16 col-md-16 col-sm-16 col-xs-16','html':[
            {'<>':'span','class':'prodname','html':'${name}'},
            {'<>':'div','class':'mT20','html':[
                {'<>':'p','html':'${info1}'},
                {'<>':'p','html':'${info2}'},
                {'<>':'p','html':'${info3}'}
            ]}
        ]}
    ]}
]};

The bold part is a image that I want to run a function showPic(img); 粗体部分是我要运行功能showPic(img);的图像; where the "img" is the 4th line of the template. 其中“ img”是模板的第四行。

The div with the "product" class is what I want to be clicked by the user and it targets the img and sends the img to showPic. 带有“产品”类的div是我想要被用户单击的对象,它以img为目标并将img发送到showPic。

I want it to target .product using jQuery. 我希望它使用jQuery定位.product。

Here's how the code is at the moment. 现在的代码如下。 http://ttrltest.000webhostapp.com/tbcl-products.html http://ttrltest.000webhostapp.com/tbcl-products.html

I tried edit With help from replies: 我试着在回复的帮助下进行编辑

$('.product').click(function(){
    showPic($(this).find('img'));
});

and I can't get the src attribute from the image element. 而且我无法从image元素获取src属性。

Neither alert or showPic are being executed because you are have syntax error. 因为存在语法错误,所以警报或showPic均未执行。

.children used without wrapping this in jquery $(), evaluates to a property not a function. 不用将其包装在jquery $()中的.children评估为属性而不是函数。 You are probably looking for the function instead. 您可能正在寻找该功能。 Additionally the children function only returns direct children, looking at your images they are several elements down in the DOM. 另外,children函数仅返回直接的children,看着您的图像,它们是DOM中的几个元素。 Use the find() instead. 使用find()代替。

$(".product").click(function(){
  showPic($(this).find('img'));
});

Edit 编辑

Careful when switching between plain Javascript and Jquery. 在纯Javascript和Jquery之间切换时要小心。 Now you are working with JQuery objects inside of your showPic method. 现在,您正在showPic方法内部使用JQuery对象。 Update your method: 更新您的方法:

function showPic(img){
    content.innerHTML = "<img src='" + $(img).attr("src") + "' alt='' class='img-responsive center-block'>";
    modal.style.display = "block";
}

Note: I tried your showPic method on your site, was getting an error "cannot set property 'innerHTML' of undefined". 注意:我在您的网站上尝试了showPic方法,但出现错误“无法设置未定义的属性'innerHTML'”。 Not sure where content is being set from, but you may want to double check. 不确定从哪里设置内容,但是您可能需要仔细检查。

I found the answer. 我找到了答案。

$(this).find('img') produces an array so I added [0].src to finally get the source. $(this).find('img')产生一个数组,所以我添加了[0].src以最终获得源代码。

If you're already using jQuery then use the jquery plugin for JSON2HTML which supports jquery embedded events. 如果您已经在使用jQuery,请使用JSON2HTML的jquery插件,该插件支持jquery嵌入式事件。 Here's how you can add a onclick attribute directly to the transform so you don't have to add it later using jquery 您可以通过以下方法将onclick属性直接添加到转换中,这样以后就不必使用jquery添加它了

var template = {'<>':'div','class':'col-lg-12 col-md-24 col-sm-24 col-xs-24','html':[
{'<>':'div','class':'product col-lg-offset-1 col-lg-22 col-md-offset-0 col-md-24 col-sm-offset-0 col-sm-24 col-xs-offset-0 col-xs-24','html':[
    {'<>':'div','class':'prodimg col-lg-8 col-md-8 col-sm-8 col-xs-8','html':[
        {'<>':'img','src':'${source}','alt':'${name}', 'class':'img-responsive'}
    ],'onclick':function(){
        //do whatever you need to here, like find your image etc..
            var $img = $(this).find('img');
        }},
    {'<>':'div','class':'prodetails col-lg-16 col-md-16 col-sm-16 col-xs-16','html':[
        {'<>':'span','class':'prodname','html':'${name}'},
        {'<>':'div','class':'mT20','html':[
            {'<>':'p','html':'${info1}'},
            {'<>':'p','html':'${info2}'},
            {'<>':'p','html':'${info3}'}
        ]}
    ]}
]}

]}; ]};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM