2015-08-19 39 views
0

我無法切換屬性contenteditable。默認情況下,我在HTML代碼中將其硬編碼爲false,當我單擊#edit-button時,它將變爲false。但是當我再次點擊它時,沒有任何反應。看起來else-從未被觸發。jQuery不會切換contenteditable點擊和/ href不禁用

我也試圖禁用每個a/button href但我在這個問題上也沒有成功。下面的if語句是錯誤的嗎?

if ($(this).is(':button')) { 
    $(this).attr('href', '#'); 
} 

的jQuery:

$('#edit-button').on('click', function() { 
    $('section').each(function() { 
     $(this).find('[contenteditable]').each(function() { 
      if ($(this).attr('contenteditable', false)) { 
       $(this).attr('contenteditable', true); 
       $('#header').focus(); 
       if ($(this).is(':button')) { 
        $(this).attr('href', '#'); 
       } 
      } else { 
       $(this).attr('contenteditable', false); 
      } 
     }); 
    }); 
}); 

試圖通過左右,但和谷歌沒有運氣搜索。

感謝您的幫助。

+0

您需要添加屬性或removeAttr作爲CONTENTEDITABLE,不要使用CONTENTEDITABLE或真或假。 –

回答

0

這是我做過什麼:

<div contenteditable="true">The quick brown fox...</div> 
<br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<a href="http://www.google.com/">Testing</a><br/> 
<br/> 
<button class="disableToggle">Button</button> 
<br/> 
<button class="disableToggle">Button</button> 
<br/> 
<button class="disableToggle">Button</button> 
<br/> 
<br/> 
<br/> 
<button id="toggle">Toggle Edit</button> 

$("#toggle").click(function() { 
    if ($("div").attr("contenteditable") == "true") { 
     $("div").attr("contenteditable", "false"); 
     $(".disableToggle").attr("disabled", "true"); 
     $("a").on("click", function(e){e.preventDefault();}); 
    } else { 
     $("div").attr("contenteditable", "true"); 
     $(".disableToggle").removeAttr("disabled"); 
     $("a").off("click"); 
    } 
}); 

Here is the JSFiddle demo

+0

這工作,謝謝。你會知道如何解決上述按鈕/ href問題嗎? –

+0

@AntonFlärd您是否還試圖禁用所有鏈接和按鈕? –

+0

@AntonFlärd我更新了答案和[JSFiddle]的鏈接(http://jsfiddle.net/anoorally/sps4rhfn/2/):) –

0

您需要使用removeAttr函數。

$(this).removeAttr('contenteditable'); 

**instead of** 

$(this).attr('contenteditable', false); 

並在檢查屬性時。您可以使用作爲

var attr = $(this).attr('contenteditable'); 
if (typeof attr !== typeof undefined && attr !== false) { 
    // Your existing code. 
} 

instead of 

if ($(this).attr('contenteditable', false)) { 

//你的全代碼如下:

$('#edit-button').on('click', function() { 
    $('section').each(function() { 
    $(this).find('[contenteditable]').each(function() { 
     var attr = $(this).attr('contenteditable'); 
     if (typeof attr !== typeof undefined && attr !== false) { 
      $(this).attr('contenteditable', true); 
      $('#header').focus(); 
      if ($(this).is(':button')) { 
       $(this).attr('href', '#'); 
      } 
     } else { 
      $(this).removeAttr('contenteditable'); 
     } 
    }); 
    }); 
}); 
0

我認爲,隨着CONTENTEDITABLE你必須設置「真」與「假」的字符串。不像其他屬性可以只是空的。 嘗試設置

$(this).attr('contenteditable', 'true'); 

和 $(本).attr( 'CONTENTEDITABLE', '假');

不是100%這對所有瀏覽器都是一樣的,但只是一槍!