Javascript 使用jQuery更改html文本
当用户单击一个按钮时,我试图更改h2的文本 Javascript:Javascript 使用jQuery更改html文本,javascript,jquery,html,Javascript,Jquery,Html,当用户单击一个按钮时,我试图更改h2的文本 Javascript: $("#GoButton").click(function() { $("#myDiv").html("Hello World"); }); 以及html: <a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a> 这应该对你有用 $("#myDiv").html("Hello W
$("#GoButton").click(function() {
$("#myDiv").html("Hello World");
});
以及html:
<a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a>
这应该对你有用
$("#myDiv").html("Hello World");
如果您只想更改文本,那么可以使用text()而不是html()方法
更新 这是可行的,您需要在jquery中对此进行适当设置:
您需要确保是否已经使用
$(function(){..})加载了DOM使用e.preventDefault()
您的javascript(在单击时绑定函数)可能执行得太早,如:
在DOM完全加载后尝试绑定事件:
$(function(){
$("#GoButton").click(function() {
$("#myDiv").html("Hello World");
});
});
这是密码
HTML:
<div id="mydiv">fd</div>
<h2 id="mydiv1">4343</h2>
<a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a>
如果您发布所有HTML,可能会更清晰。根据猜测,以下是我认为你想要的:
<html>
<body>
<div id="myDiv">
<h2 id="myHeader">Click Go</h2>
<p><a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a></p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function() {
$("#GoButton").click(function() {
$("#myHeader").html("Hello World");
});
});
</script>
</body>
</html>
单击“开始”
$(窗口)。加载(函数(){
$(“#GoButton”)。单击(函数(){
$(“#myHeader”).html(“你好世界”);
});
});
函数here()
{
//警报(document.getElementById(“myHeader”).innerHTML);
document.getElementById(“myHeader”).innerHTML=“Hello World”;
}
标题
去
希望您能从中有所收获。到底是什么问题?请提供JSFIDDL中的代码您想更改标记中的文本吗?您的代码是正确的,$(“#myDiv”).html(“Hello World”)代码>。检查您是否在html中包含了jquery库,id对于所有元素都应该是唯一的。在用户单击按钮后,#myDiv(h2元素)中的文本没有得到我想要的值。您需要加载jquery库,该库位于左侧面板下拉列表中,在我的答案中的脚本之前,在html页面中添加此
。有关更多信息,请参阅。@ItsikMauyhas-这是有效的。您需要在jquery中对此进行适当的设置:谢谢!这就解决了问题!如果我有15个以上的代表,我会向上回答。
$(function(){
$("#GoButton").click(function() {
$("#myDiv").html("Hello World");
});
});
<div id="mydiv">fd</div>
<h2 id="mydiv1">4343</h2>
<a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a>
$("#GoButton").click(function() {
$("#mydiv1").html("Hello World");
$("#mydiv").html("Hello World");
});
<html>
<body>
<div id="myDiv">
<h2 id="myHeader">Click Go</h2>
<p><a href="#jobResPage" data-role="button" data-inline="true" id="GoButton">GO</a></p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function() {
$("#GoButton").click(function() {
$("#myHeader").html("Hello World");
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function here()
{
//alert(document.getElementById("myHeader").innerHTML);
document.getElementById("myHeader").innerHTML = "Hello World";
}
</script>
</head>
<body>
<div id="myDiv">
<h2 id="myHeader">Header</h2>
<button id="GoButton" onclick="here();">GO</button>
</div>
</body>
</html>