我正在尝试使用javascript和css创建一个小工具,允许用户使用鼠标选择文本区域(整个单词,单词的一部分,单个字符),然后单击一个按钮,然后将其加粗选择.
我有2个工作:我可以加粗整个区域,我可以将鼠标选择写入控制台.
但我无法弄清楚如何只粗体化用鼠标选择的文本部分.
我在这里有一个jsfiddle:http: //jsfiddle.net/75EwZ/4/
这是代码:
$(".boldtrigger").click(function() {
var selection = getSelText();
console.log('selected value', selection);
//only bold text that is selected
//get contents of .text
//replace selected section with <span class="selectedText"></span>
//ex: user selects the word "the" with the mouse:
//<div class="text">Testing the waters</div> becomes:
//<div class="text">Testing <span class="selectedText">the</span> waters</div>
//then bold contents of .selectedText
$(".text").toggleClass("bold");
});
function getSelText()
{
var txt = '';
if (window.getSelection)
{ txt = window.getSelection(); }
else if (document.getSelection)
{ txt = document.getSelection(); …Run Code Online (Sandbox Code Playgroud) 我要做的是创建一个脚本,可以将用户输入的任何数字输入一个字段并将其乘以100,并在按下按钮时将其输出到控制台.我已经部分完成但我不熟悉console.log和一个整体javascript的新手.我一直在读书,但我似乎无法弄清楚它应该如此简单.它只有1个字段+ 1个按钮.
这是我的HTML:
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Java Help</h1>
<input id="Variables"/>
<input type="button" onclick="getInput()"/>
<script src="js/main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的Javascript在这里:
/**
* Created by Jabrian on 1/24/2015.
*/
function getInput(){
var userInput= document.getElementByID("Variables").value;
console.log("100" * userInput);
Run Code Online (Sandbox Code Playgroud)