簡體   English   中英

如何使用jquery更改按鈕單擊時關注的contenteditable元素類?

[英]How to change class of focused contenteditable element on button click using jquery?

我正在嘗試更改位於contenteditable div中的元素的類

我目前的代碼是:

<button>swap class</button>
<div contenteditable="true">
    <h1 class="line-height-1">Your Headline Here</h1>
</div>    
<div contenteditable="true">
    <h1 class="line-height-1">Your Headline Here</h1>
</div>

我想在單擊按鈕時將類.line-height-1替換為當前所選可編輯div的另一個.line-height-1

有人可以幫忙嗎?

當您單擊按鈕時,聚焦元素會模糊。 您需要將焦點元素存儲在變量中,並在按鈕單擊時更改它。 按下按鈕后使其聚焦。

 var element; $("div[contenteditable]").focus(function(){ element = this; }); $("button").click(function(){ $(element).focus().find("h1").removeClass("line-height-1").addClass("newClass"); }); 
 .line-height-1 { color: red } .newClass { color: blue } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>swap class</button> <div contenteditable="true"> <h1 class="line-height-1">Your Headline Here</h1> </div> <div contenteditable="true"> <h1 class="line-height-1">Your Headline Here</h1> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM