简单的JavaScript.
var userName = prompt ("What's your name?");
document.write ("Hello " + userName + ".");
Run Code Online (Sandbox Code Playgroud)
获取用户输入(名称)的方法是否比通过弹出框更方便?我如何在网站中实现文本框以获取用户名?
这怎么样?
<section class="content">
<input id="name" type="text">
<button onclick="userName()">Submit</button>
<p id="user"></p>
<script>
function userName() {
var name = document.getElementById("name").value;
user.innerHTML = "Hello" + " " + name}
</script>
</section>
Run Code Online (Sandbox Code Playgroud) 创建了一个导航栏,在Firefox中看起来不错,然后看到,在chrome和ieres滚动条.如何设置导航,以便没有滚动条?
header {
width: 800px;
margin-left: auto;
margin-right: auto;
padding-top: -7px;
margin-top: -8px;
}
.nav {
width: 600px;
line-height: 0px;
margin-left: auto;
margin-right: auto;
}
.nav div {
display: block;
float: left;
width: 20%;
padding: 0px;
margin: 0px;
.nav img a {
padding-right: 10px;
}Run Code Online (Sandbox Code Playgroud)
<header>
<div id="wrapper">
<div class="nav">
<div>
<a href="index.html"><img width="103px" border="0" height="24" src="nav-01_over.gif"></a>
</div>
<div>
<a href="company.html"><img src="nav-02.gif" width="103px" border="0" height="24"></a>
</div>
<div>
<a href="products.html"><img src="nav-03.gif" width="103px" border="0" height="24"></a>
</div>
<div>
<a href="solutions.html"><img src="nav-04.gif" width="103px" border="0" …Run Code Online (Sandbox Code Playgroud)构建了一个小费计算器,但希望通过获取正在单击的按钮的 innerHTML 来缩短代码。
Total Bill: <input id="bill" type="text">
<button type="button" onclick="tip15()">15</button>
<button type="button" onclick="tip18()">18</button>
<button type="button" onclick="tip20()">20</button>
<div id="tip">You owe...</div>
function tip15(){
var totalBill = document.getElementById("bill").value;
var totalTip = document.onclick.innerHTML
var tip = totalBill * 0.15;
document.getElementById("tip").innerHTML = "$" +tip
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是我必须写出三个版本的函数来对应小费金额。我想创建一个函数来获取被单击的按钮的 innerHTML 并在函数中使用该值。我希望它看起来像这样
function tip(){
var totalBill = document.getElementById("bill").value;
**var totalTip = GET INNERHTML OF CLICKED BUTTON**
var tip = totalBill * ("." + totalTip);
document.getElementById("tip").innerHTML = "$" +tip
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以在不同的按钮上运行相同的功能。