[英]Jade variable rendering
伙計們,我如何訪問我在腳本中設置的變量。 ?
#someModal.modal.fade.large
.modal-dialog(style="width:70%")
.modal-content
.modal-header
button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
h2.modal-title #{someVariable}
.modal-body
h3
...
script.
| var someVariable = #{someVariable};
script.
$('body').on('show.bs.modal', '.modal', function () {
var someVariable = 'test';
});
我通常使用的解決方法是將變量附加到窗口:
#someModal.modal.fade.large
.modal-dialog(style="width:70%")
.modal-content
.modal-header
button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
h2.modal-title #{someVariable}
.modal-body
h3
...
script
| var someVariable = #{someVariable};
script.
$('body').on('show.bs.modal', '.modal', function () {
var someVariable = 'test';
});
請注意,如果變量是Object
(或Array
),則需要對變量進行JSON.stringify
。
在您的頁面加載之前,您的腳本將不會開始運行,因此jade永遠無法訪問客戶端上設置的變量。 您應該使用jQuery來修改HTML元素。
script.
var $modal = $('#someModal')
, $title = $modal.find('.modal-title');
$modal.on('show.bs.modal', '.modal', function () {
var someVariable = 'test';
$title.text(someVariable);
});
$modal.modal();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.