gpt4 book ai didi

jquery - 我怎样才能让这个简单的 JQuery 脚本更短更好

转载 作者:行者123 更新时间:2023-12-01 06:45:11 25 4
gpt4 key购买 nike

我制作了简单的脚本来更改选择选项上的标题。但我认为我的代码对于这个简单的脚本来说太长了。我想让同一个脚本看起来更好更短

HTML:

<div class="main_text">
<h1>Change Headings</h1>
</div>


<select id="selection">
<option value="h1">H1</option>
<option value="h2">H2</option>
<option value="h3">H3</option>
<option value="h4">H4</option>
<option value="h5">H5</option>
</select>

JQuery:

$(函数(){

    $('select').change(function(){
if ($(this).val() == 'h1') {
$(".main_text").html("<h1>Change Headings</h1>")
}

if ($(this).val() == 'h2') {
$(".main_text").html("<h2>Change Headings</h2>")
}

if ($(this).val() == 'h3') {
$(".main_text").html("<h3>Change Headings</h3>")
}

if ($(this).val() == 'h4') {
$(".main_text").html("<h4>Change Headings</h4>")
}

if ($(this).val() == 'h5') {
$(".main_text").html("<h5>Change Headings</h5>")
}
});

});

Jsfiddle

最佳答案

你可以这样做:-

$('#selection').change(function () {
var tag = $(this).val();
$(".main_text").html("<" + tag + ">Change Headings</" + tag + ">");
});

FIDDLE DEMO

关于jquery - 我怎样才能让这个简单的 JQuery 脚本更短更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com