点击时按钮消失 [英] Button disappears on click
问题描述
我有一个非常简单的 HTML
文件(它是 MVC 4
项目的一部分,但我也在简单的 HTML
文件)
包含两个按钮和一些 jquery
脚本:
I have a very simple HTML
file (it's part of an MVC 4
project but i also tested on a plain HTML
file)
that contains two buttons and some jquery
script:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div>
<button id="btn1">Get a string</button>
<br />
<p id="p1" style="font-size: 12px" />
<br />
<button id="btn2">Get user agent</button>
<br />
<p id="p2" style="font-size: 12px" />
<br />
</div>
<script>
$(function () {
$('#btn1').click(function () {
$('#p1').text('clicked');
});
});
</script>
<script>
$(function () {
$('#btn2').click(function () {
$('#p2').text(navigator.userAgent);
});
});
</script>
</body>
点击第二个按钮后效果很好,但当点击第一个按钮( btn1
)时,第二个按钮会消失。
我尝试切换它们并更改脚本的实现:
after clicking the second button everything works great, but when clicking the first button (btn1
) the second one disappears.
I tried switching them and changing the implementation of the script:
<script>
$(function () {
$('#btn1').click(function () {
$('#p1').text('clicked');
});
});
$(function () {
$('#btn2').click(function () {
$('#p2').text(navigator.userAgent);
});
});
</script>
和:
and:
<script>
$(document).ready(function () {
$('#btn1').click(function () {
$('#p1').text('clicked');
});
});
$(document).ready(function () {
$('#btn2').click(function () {
$('#p2').text(navigator.userAgent);
});
});
</script>
但没有任何变化。
有关如何解决它的任何想法? p>
but nothing change.
any ideas on how to solve it?
推荐答案
问题是您没有关闭您的< p>
标签正常。您做了< p .... />
,但它需要像这样格式化< p ...>< / p>
。
The problem is that you did not close your <p>
tags properly. You did <p .... />
but it needs to be formatted like this <p ...></p>
.
请参阅:
http://codepen.io/AlienHoboken/pen/kinGq
这篇关于点击时按钮消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!