-1

I'm pretty new with coding, so bear with me. I've got a button on a page that, if I press on it, should make the specific text bold.

But for some reason it doesn't work and I have no clue why..

Here is an example:

HTML

<input type="button" class="btn" onclick="boldtext();" />
<br />
<div id="text">This is some text</div>

Javascript

function boldtext () {
document.getElementById('text').style.fontWeight = 'bold';
}

My problem is when I click on the button, nothing happens.

Did I make a mistake somewhere?

4

4 回答 4

3

在标签中编写 JavaScript 代码<head>。您的代码正在运行。见演示

在此处输入图像描述

于 2013-08-27T11:23:30.983 回答
-1

希望你已经把上面的脚本包装在里面<head>

<head>
<script>
function boldtext () {
document.getElementById('text').style.fontWeight = 'bold';
}
</script>
</head>
于 2013-08-27T11:28:55.840 回答
-1

最简单的 jQuery 解决方案是

function boldtext () {
    $('#text').css('font-weight','bold');
}

或者

您可以使用jQuery event处理程序。

$('input.btn').click(function(){
    $('#text').css('font-weight','bold');
});
于 2013-08-27T11:22:01.000 回答
-1

如果你想使用 jQuery,你应该创建一个事件处理程序而不是使用内联事件

HTML

<input type="button" class="btn" id="your_button" />
<br />
<div id="text">This is some text</div>

JS

$('#your_button').click(function(){
    $('#text').css('font-weight','bold');
});
于 2013-08-27T11:22:24.203 回答