简体   繁体   中英

how to iterate through a particular attribute using js?

I am using ckeditor inline on a webpage that i have built.I can save the data to a database but the major problem is that ckeditor applies its own classes and other attributes to all the elements on which contenteditable is set to true.Also it removes some classes on elements. This is affecting the styling of my webpage. I wish to remove the classes and other attributes set by ckeditor before printing it on the browser.For this I need to loop through all the elements having content editable set to true. How can we do this using js?

Plain JS:
var el = document.getElementsByTagName("*");
for(var i = 0, l = el.length; i < l; i++)
{
if (el[i].contentEditable)
{
// Do here what you want
}
}

jQuery:

as dandavis said:

$("*[contentEditable]").each(function()
{
    // 
});

Edit:

As far as the .contentEditable does not return boolean, you must check whether the element has the contentEditable attribute like this:

var el = document.getElementsByTagName("*");

for(var i = 0, l = el.length; i < l; i++)
{
    if (el[i].contentEditable === "true" || el[i].contentEditable === "")
    {
        // Do here what you want
    }
}

In order to remove the contentEditable attribute:
In jQuery you can do it like this:

$("*[contentEditable]").each(function()
{
    this.prop("contentEditable", false);
});

Plain js:

el[i].contentEditable = "false";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM