我正在尝试在输入文本字段中获取粗体特定文本。我不确定如何去做,因为 html 代码不会在文本字段中解释,所以像 <b>
这样的东西不会工作。是否可以只加粗一些文字? bold() 之类的方法只添加 <b>
围绕字符串。
谢谢
最佳答案
这是一个技巧。
INPUT 字段位于 SPAN 之上。有一个函数示例,将前 3 个字符设为粗体。 使用旧版浏览器可能会遇到透明度问题。在这种情况下,您可以保留没有粗体效果的普通输入字段。
<!DOCTYPE html>
<html lang="en">
<head>
<title>transp</title>
<style>
div{
position:relative;
}
input, span{
top:0;
position:absolute;
width:120px;
}
span{
top:2px;
left:2px;
z-index:1;
overflow:hidden;
}
input{
background:transparent;
z-index:2;
}
</style>
</head>
<body>
<div>
<input onkeyup="bold3(this)" />
<span id="back"></span>
</div>
<script>
function bold3(inp){
inp.style.color = 'transparent';
var span = document.getElementById('back');
span.innerHTML =
'<b>' +
inp.value.substr(0, 3) +
'</b>' +
inp.value.substr(3);
}
</script>
</body>
</html>
关于javascript - 如何将输入文本字段中的一些文本设为粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10839279/