开发者

Style textarea content with the same style as any other element using JavaScript?

开发者 https://www.devze.com 2023-02-21 23:58 出处:网络
I would like to dynamically insert textareas into different parts of my site and have the styling of the text in the textarea match the style of the normal text in that element. Is it possible to do t

I would like to dynamically insert textareas into different parts of my site and have the styling of the text in the textarea match the style of the normal text in that element. Is it possible to do this with JavaScript? I thought that perhaps I could loop through all the obj.style properties and just apply them to the textarea.

For example, the font used in the textarea's on stackoverflow is a 10开发者_JAVA百科px(?) mono-spaced font while the final content text looks like 14px Arial.


The best way to do this is through a CSS class. Either change the style of all textareas with textarea { /* CSS */ } or use a special class for them like textarea.dynamicallyInserted { /* CSS */ } with <textarea class="dynamicallyInserted"></textarea>


Using jquery, you can enumerate as many css properties as you want and set them on the element you're adding:

function addTextArea(targetId) {
    $('#' + targetId).add('textarea')
        .css('font-family', $('#' + targetId).css('font-family'))
        .css('font-size', $('#' + targetId).css('font-size')); //etc...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消