Jquery 单击替换Div内容
我对jQuery一无所知,但我想实现一些真正重要的东西Jquery 单击替换Div内容,jquery,html,css,Jquery,Html,Css,我对jQuery一无所知,但我想实现一些真正重要的东西 我想有一个按钮/链接来替换div内容,如果我再次按下该按钮,它会将原始内容放回原位。这里有一个方法: HTML: <div id="1"> My Content 1 </div> <div id="2" style="display:none;"> My Dynamic Content </div> <button id="btnClick">Click me!&
我想有一个按钮/链接来替换div内容,如果我再次按下该按钮,它会将原始内容放回原位。这里有一个方法:
HTML:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
<div id="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br>
<input type="text" id="replacementtext" />
<input type="button" id="mybutt" value="Swap" />
<input type="hidden" id="vault" />
//Declare persistent vars outside function
var savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
JsFiddle:
一个简单的addClass和removeClass就可以满足您的需要
$('#change').on('click', function() {
$('div').each(function() {
if($(this).hasClass('active')) {
$(this).removeClass('active');
} else {
$(this).addClass('active');
}
});
}))
Seee
我建议您在使用jquery之前先学习它。编辑:这个答案是在OP的JSFIDLE示例发布之前提交的。请参阅第二个答案,了解对JSFIDLE的响应
下面是它如何工作的一个示例: HTML:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
<div id="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br>
<input type="text" id="replacementtext" />
<input type="button" id="mybutt" value="Swap" />
<input type="hidden" id="vault" />
//Declare persistent vars outside function
var savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
使用JSFIDLE示例:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
<div id="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br>
<input type="text" id="replacementtext" />
<input type="button" id="mybutt" value="Swap" />
<input type="hidden" id="vault" />
//Declare persistent vars outside function
var savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
JSFIDLE很好,但是在event.preventDefault()语句的末尾缺少分号
这项工作:
第三个答案 对不起,也许这次我答对了
试试这个:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
<div id="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br>
<input type="text" id="replacementtext" />
<input type="button" id="mybutt" value="Swap" />
<input type="hidden" id="vault" />
//Declare persistent vars outside function
var savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
我想你想要这样的东西
HTML:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
<div id="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br>
<input type="text" id="replacementtext" />
<input type="button" id="mybutt" value="Swap" />
<input type="hidden" id="vault" />
//Declare persistent vars outside function
var savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
JsFiddle:
为了实现这一点,这里有一个完整的解决方案-
函数toggleText(){
如果($(“#txt-1”).css(“显示”)!=“无”){
$(“#txt-1”).css(“显示”、“无”);
$(“#txt-2”).css(“显示”、“块”);
$(“#txt-3”).css(“显示”、“无”);
}else if($(“#txt-2”).css(“显示”)!=“无”){
$(“#txt-1”).css(“显示”、“无”);
$(“#txt-2”).css(“显示”、“无”);
$(“#txt-3”).css(“显示”、“块”);
}否则{
$(“#txt-1”).css(“显示”、“块”);
$(“#txt-2”).css(“显示”、“无”);
$(“#txt-3”).css(“显示”、“无”);
}
};
切换
你好
你好吗
再见
请发布一些代码,以及到目前为止您尝试过的内容……我不懂javascript,这就是我没有编写代码的原因。我发现一个堆栈溢出问题接近我的需要。我把它放在JS fiddle中,并修改了一些代码,它正在工作,但如果我再次单击链接,我不知道如何将原始内容放回。这是我的fiddle链接,谢谢。你的小提琴离我很近。您在JS中添加了“这是我的动态内容”,但我需要一个HTML div,它将在单击时显示并替换。。你能帮我调整一下你的小提琴吗?是的,那把太完美了。。我看到你为此设置了onload,我将如何在我的Wordpress网站中插入代码。又一个愚蠢的问题。。我不知道如何制作这样的4个代码。。你能用小提琴演奏吗。。我们将不胜感激。。这是注释的代码,我已经试着说清楚了,如果你有任何问题告诉我,请注意,你可以在任何元素上使用事件,比如,如果你用一个div ID替换btnClick,当它检测到对div的点击时,它也会起作用。谢谢大师!在你的帮助下,我想出了解决办法。。这是我需要的@JunaidKhawaja,看来你明白了:不,我需要一些在BernardoLima评论中定义的东西。。请看那边的评论。。我需要更多的帮助,但我想他已经下线了。。