如何在类中使用getElementById获取值并在其他类中使用它 [英] How to get the value with getElementById in a class and use it in a different class

查看:180
本文介绍了如何在类中使用getElementById获取值并在其他类中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<body>
    <div class="container">
        Input
        <form class="form-inline" role="form">
            <div class="form-group">

                <input type="text" class="form-control" id="getString" placeholder="Enter some string">

            </div>
            <button type="button" class="btn btn-primary" id="nowBtn">Now</button>
        </form>

        <br/>
        <br/>

        All Headings<textarea class="form-control" rows="6"></textarea>
    </div>   
</body>

我正在使用引导程序,当按下立即"按钮时,我将在表单中提供一些字符串.
我应该在textarea中获得该字符串的<h1>标签(即在textarea中应用<h1>标签的<h1>some string</h1>).

I am using bootstrap and I will give some string in the form, when the Now button is pressed.
I should get the <h1> tag of that string in the textarea (i.e <h1>some string</h1> in the textarea with applied <h1> tag ).

这是可以实现的吗?我想使用jQuery.

Is it achievable? I want to use jQuery.

推荐答案

根据您的评论,我了解到您希望将textarea中的font-size设置为与h1标签相同的大小.

From your comments I've understood you'd like to set the font-size in the textarea to same size as h1 tag would have.

由于HTML中没有h1标记,因此您需要在#nowBtn的click事件处理函数中创建一个标记:

Since there's no h1 tag in your HTML, you need to create a one in the click event handler function of the #nowBtn:

var header = document.createElement('h1'),
    size = window.getComputedStyle(header, null).fontSize; // Depending used browser and CSS, this returns for example 32px

然后您可以像这样设置textareafont-size:

Then you can set the font-size of textarea like this:

$('textarea').css('font-size', size);

jsFiddle上的实时演示.

编辑

正如bfavaretto所提到的,跨浏览器的方式是使用jQuery来获取h1的大小:

As bfavaretto has mentioned, a cross-browser way would be to use jQuery to get the size of the h1:

size = $(header).css('font-size');

这篇关于如何在类中使用getElementById获取值并在其他类中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆