2012-08-29 45 views
0

我想在另一個之前渲染一個div。某些js文件在Drupal中不工作

我測試了我本地機器上的js,以確保它通過創建簡單頁面來工作。代碼如下:

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
    <div id="QWERTY"> 
     <div class="content"> 
      <div class="field-name-field-image-one">IMAGE</div> 
      <div class="body">BODY</div> 
     </div> 
    </div>  
<script>$('.content .body').insertBefore('.content .field-name-field-image-one'); // check before() examples</script> 

</body> 
</html> 

我已經添加下面的代碼到一個js文件在我的Drupal網站:

(function ($, Drupal, window, document, undefined) { 

    $('.content .body').insertBefore('.content .field-name-field-image-one'); // check before() examples 

})(jQuery, Drupal, this, this.document); 

我.info文件鏈接到js和它加載。其他需要javascript的元素也在工作。我甚至嘗試在html.tpl文件中添加腳本,但那也不起作用。

我試圖影響的div嵌套在html標記的深處,並且與我所做的簡單版本相比,它們有幾個類適用於它們。這可能是原因嗎?我不夠具體嗎?

回答

0

由於一些幫助,我已經將它更新到以下內容,它的工作原理:

(function ($, Drupal, window, document, undefined) { 
    $(function() { 
    $('.content .body').insertBefore('.content .field-name-field-image-one'); // check before() examples 
    }); 

})(jQuery, Drupal, this, this.document); 
0

你並不需要使用Javascript功能來改變內容類型字段的順序。

如果您使用的是Drupal 7,請轉到主頁»管理»結構(/ admin/structure/types),然後單擊要更改的內容類型旁邊的「manage display」。您現在可以拖放字段以更改其每個查看模式的顯示順序(例如,默認,Teaser)。

如果你真的想用Javascript來做到這一點,Drupal 7使用特殊的語法。

(function ($) { 
    Drupal.behaviors.custom_moveimage = { 
    attach: function (context, settings) { 
     $('.content .body').insertBefore('.content .field-name-field-image-one'); 
    } 
    }; 
}(jQuery)); 

Drupal文檔有關於此的more information

相關問題