我打算使用contenteditable="true"
div,以便我可以顯示wp_content
的內容但是一旦修改它需要通過<textarea>
提交我正在使用以下jQuery來更新文本區域在可編輯的div中進行任何編輯。使用php時jQuery函數中斷php
jQuery("#contentEdit").keyup(function(event) {
value = jQuery('#contentEdit').html();
jQuery("#myTextarea").val(value);
});
而且我有與此相關的元素。
<div id="contentEdit" contenteditable="true">
<h1>some editable content</h1>
</div>
<textarea class="form-control" id="myTextarea" name="Description" rows="3">
<h1>some editable content</h1>
</textarea>
這工作得很好。但是,當我在<?php the_content(); ?>
嘗試添加甚至echo the_content();
它打破了,我無法獲得jQuery代碼進行任何更新。
一個例子是:
<div id="contentEdit" contenteditable="true"><?php echo the_content(); ?></div>
編輯:我得到這個工作完全在一個新的一頁 - 它似乎不會在引導模式下工作。
我的jQuery現在看起來是這樣的:
jQuery(document).ready(function() {
jQuery(document).on('keyup','#editableContent',function(){
value = jQuery('#editableContent').html();
jQuery("#myTextarea").val(value);
});
});
EDIT2 現在我知道我需要唯一的ID,我嘗試使用以下。
var editableID = 'editableContent<?php echo the_ID(); ?>';
var textAreaID = 'myTextarea<?php the_ID();?>';
jQuery(document).ready(function() {
jQuery(document).on('keyup', editableID ,function(){
value = jQuery(editableID).html();
jQuery(textAreaID).val(value);
});
});
編輯3
我終於得到這個工作使用下列內容:
jQuery(document).ready(function() {
jQuery(document).on('keyup', '#editableContent<?php echo the_ID(); ?>' ,function(){
value = jQuery('#editableContent<?php echo the_ID(); ?>').html();
jQuery('#myTextarea<?php echo the_ID(); ?>').val(value);
});
});
是相當新的這個它會感激,如果有人可以優化該代碼?並建議我如果這是可憐的形式....
非常感謝提前。
請發帖'the_content()'函數。 確保此功能不會產生具有相同ID的多個div – DarkBee 2014-11-02 11:28:10
這是我現在正在處理的問題DarkBee在此情況下的搜索結果循環20個結果,爲每個結果創建一個唯一的模式 - 但它們具有相同的ID可編輯區域。 這意味着它只適用於第一個結果。從WP'the_ID'向每個可編輯區域添加一個唯一ID會是一種不錯的形式嗎?如果是的話,我將如何在jQuery中實現這一點? – Taylorsuk 2014-11-02 11:33:12
這就是爲什麼它不起作用。 ID必須是唯一的。嘗試改爲上課並在此之後使用'.editableContent'selector – DarkBee 2014-11-02 11:34:45