如何在类中使用getElementById获取值并在其他类中使用它 [英] How to get the value with getElementById in a class and use it in a different class
问题描述
<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
然后您可以像这样设置textarea
的font-size
:
Then you can set the font-size
of textarea
like this:
$('textarea').css('font-size', size);
编辑
正如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屋!