使用javascript获取div之间的属性值

时间:2016-10-31 03:29:22

标签: javascript jquery html dom

我需要在data-type =" image-link"之后得到href和src值。在下面的代码中

import json

d = {'Shake': '$4', 'Fries': '$3', 'Soda': '$2', 'Burger': '$5'}
with open('file.dat', 'w') as f:
    json.dump(d, f)

with open('file.dat', 'r') as f:
    print json.load(f)

我正在尝试这样做以获得我想要的价值:

<div id="newsletter-builder-area-center-frame-content">
    <div class="sim-row"  data-id="1" hola="1">
        <div class="sim-row-header1">
            <div class="sim-row-header1-nav">
                <div class="sim-row-edit"  data-type="image-link">
                    <a href="#"><img src="https://dummyimage.com/550x150/83cfcc/fff"/></a>
                </div>
            </div>
            <div class="sim-row-header1-slider">
                <div class="sim-row-header1-slider-left">
                    <div class="sim-row-header1-slider-left-title sim-row-edit" data-type="title">Template Builder</div>
                    <div class="sim-row-header1-slider-left-text sim-row-edit" data-type="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vitae ullamcorper orci. Nullam nulla felis, imperdiet ac sollicitudin et, malesuada sagittis lacus.</div>
                </div>
                <div class="sim-row-header1-slider-right sim-row-edit" data-type="image-link"><a href="#"><img src="https://dummyimage.com/200x167/83cfcc/fff" /></a></div>
            </div>
        </div> <!-- end sim-row-header1 -->
    </div> <!-- end sim-row -->
</div>

显示&#34; Undefined&#34;如何在data-type =&#34; image-link&#34;之后得到值href和src;使用javascript请建议任何解决方案,。

有人可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我想你可能想在你的代码中使用这段代码:

  

否则if(type_source ===&#39; image-link&#39;)

代码:

else if (type_source === "image-link") {
     var descendants = source.children;
     var href_source = descendants[0].getAttribute('href');
     var source_image = descendants[0].children[0].getAttribute('src');
     console.log(href_source);
     console.log(source_image);

}