2016-10-31 47 views
0

我需要href和後SRC值在下面的代碼得到使用JavaScript

<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> 

數據類型=「圖像鏈接」我想是這樣獲得的值我想要什麼屬性的div之間的值:

var elements = document.getElementById("newsletter-builder-area-center-frame-content").querySelectorAll("div.sim-row.ui-draggable"); 

for (var i = 0; i < elements.length; i++) { 
    var element = elements[i]; 
     var hola = element.getAttribute("hola"); 
     alert("Name: "+hola+"\r\n"); 
     // var data_template['id_template']=hola; 
     var source_ = element.querySelectorAll('.sim-row-edit'); 
     alert(source_.length); 
      for (var ab = 0; ab < source_.length; ab++) { 
      var source = source_[ab]; 
      var type_source=source.getAttribute("data-type"); 
      if (type_source === "image"){ 
       var value=source.getAttribute("src"); 
       alert(source); 
       alert(value); 
      } 
      else if (type_source === "image-link"){ 
       // $(this).parent().parent(); 
       var href_source=source.nextSibling.getAttribute('href'); 
       var source_image=source.nextSibling.getAttribute('src'); 
       alert(href_source); 
       alert(source_image); 
      } 
      else if(type_source === "link"){ 
       var url=source.getAttribute("href"); 
       var text_url=source.textContent; 
       var value=url+';'+text_url; 
       alert(value); 
      } 
      else if(type_source === "text"){ 
       var value=source.innerHTML; 
       alert(value); 
      } 
      else if(type_source === "title"){ 
       var value=source.textContent; 
       alert(value); 
      } 
      // var data_template[type_source_+ab]=value; 
     } 
    } 

它顯示「未定義」如何讓數據類型=「圖像鏈接」使用JavaScript,請提出任何解決方案後價值的href和src ,.

任何人都可以幫忙嗎?

謝謝!

回答

0

我想你可能想使用你這裏面的代碼:

否則,如果(type_source === '圖像鏈接')

代碼:

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); 

} 
+0

工作完美像我想..謝謝你。 –