所以我想知道什麼是「規範」或「適當」的方式來處理通過ajax使用的HTML。正確的方法來處理ajax HTML塊
例如,我是否應該在使用它的實際頁面中保留所有的HTML?或者我應該只是一個Ajax調用來加載它?
由於減少了一個負載,在保持頁面加載性能方面是否有性能提升?或者,在頁面加載時加載額外的數據會將其取消。
下面是一個屏幕截圖,說明我的意思。您可以看到根據用戶提供的內容(當然是有限字符)更改的{name}。
任何幫助/意見表示讚賞!謝謝!
對於那些要價的部分源:
<!-- text field -->
<div class="add-field-wrapper float-left">
<input type="radio" value="text" name="input_type" id="rad-type-text" class="type-radio-btn">
<label for="rad-type-text" class="radio-lbl" data-tooltip="Used for simple inputs such as: <b>Phone Number</b> or <b>Email Address</b>">
<img src="https://placeholdit.imgix.net/~text?txtsize=16&txt=70x70&w=70&h=70" class="field-type-icon" />
<div class="field-type-text">Text Field</div>
</label>
</div>
<!-- select -->
<div class="add-field-wrapper float-left">
<input type="radio" value="select" name="input_type" id="rad-type-select" class="type-radio-btn">
<label for="rad-type-select" class="radio-lbl" data-tooltip="Use this option when you need to provide a list of choices for the user." >
<img src="https://placeholdit.imgix.net/~text?txtsize=16&txt=70x70&w=70&h=70" class="field-type-icon" />
<div class="field-type-text">Select Menu</div>
</label>
</div>
<!-- textarea -->
<div class="add-field-wrapper float-left">
<input type="radio" value="textarea" name="input_type" id="rad-type-textarea" class="type-radio-btn">
<label for="rad-type-textarea" class="radio-lbl" data-tooltip="The textarea field will appear as a <b>WYSIWIG</b> (What you see is what you get) editor. This allows for some customization of the appearance of the input.">
<img src="https://placeholdit.imgix.net/~text?txtsize=16&txt=70x70&w=70&h=70" class="field-type-icon" />
<div class="field-type-text">Text Area</div>
</label>
</div>
編輯:
<!-- Datepicker HTML block - used in JS -->
<div id="datepicker_html" style="display: none;">
<div id="{name}-block" class="datepicker-wrapper form-input-wrapper">
<div class="template-drag-handle"><img src="images/design/up-down-icon.png" class="template-drag-handle-icon" alt="Drag" /></div>
<div class="inputs-wrapper">
<div class="form-row"><input type="text" name="{name}" class="input-datepicker" placeholder="{placeholder}" id="{name}"/></div>
</div>
<?php echo $default_template_chkbox_options_html; ?>
</div>
</div>
這是一個在HTML的 「作品」。它被加載到JS變量:
這是什麼處理它 - 添加名稱,更改佔位符(只要你想,這些可重複使用多次)
function addDatePickerField(){
//Get the HTML
var datepicker_html = $('#datepicker_html').html();
datepicker_html = datepicker_html.replaceAll(/{name}/g, input_name_underscores);
datepicker_html = datepicker_html.replaceAll('{placeholder}', input_name);
$('#template-fields-wrapper').append(datepicker_html);
wrapUpAddInput('datepicker');
}
我剛纔didnt它是否會更好做一個Ajax調用,存儲「外部」的HTML,並調用它在我需要的時候 - 就像那個日期選擇器HTML塊一樣,將存儲在單獨的文件中,然後在鏈接上加載到DOM中。
我不知道你問這裏...你問情況是否好轉通過Ajax調用加載HTML或將其硬編碼到HTML文件?另外,{name}表達式是什麼?是從angular.js什麼的?或者你是如何設法使用它的? – OzzyTheGiant
與大多數事情一樣:這取決於。我通常只將*數據*返回給AJAX請求,並將它所屬的數據放在javascript中,但在某些情況下,傳回完整的HTML是有意義的。 –
**對於所有用戶來說,嵌入開發者控制檯的圖片是一個不錯的主意**:無論是從「查看源代碼」複製粘貼實際標記還是更加實用和簡單,或者從您的控制檯。 –