Javascript 向文本添加html标记将禁用按钮功能
请参阅下面的代码。我有整页的部分,每个部分都有一个按钮。按下按钮后,我希望页面进入下一节。除了我在Javascript 向文本添加html标记将禁用按钮功能,javascript,jquery,html,Javascript,Jquery,Html,请参阅下面的代码。我有整页的部分,每个部分都有一个按钮。按下按钮后,我希望页面进入下一节。除了我在或标记中包含了该部分的文本外,一切都很顺利。例如,请参见第1节和第2节,由于某种原因,在我添加这些标记后,按钮不起作用。它正在其他部门工作。即使在第1节中,如果我删除标记,它也可以正常工作。这些标签与按钮的功能有什么关系 <!DOCTYPE html> <html> <head> <title>Selecting multiple DIV ta
或
标记中包含了该部分的文本外,一切都很顺利。例如,请参见第1节和第2节,由于某种原因,在我添加这些标记后,按钮不起作用。它正在其他部门工作。即使在第1节中,如果我删除
标记,它也可以正常工作。这些标签与按钮的功能有什么关系
<!DOCTYPE html>
<html>
<head>
<title>Selecting multiple DIV tags with jquery</title>
<script type="text/javascript" src="./jquery.js">
</script>
<style type="text/css">
html{
overflow: hidden;
}
body {
backround-color: rgb(250, 250, 250);
}
h1 {
font-size: 48px;
color: rgb(90,90,90);
}
.slide {
display: block;
position: absolute;
border-style: solid;
border-width: 1px;
top: 0px;
left: 0px;
padding:20px;
height: 95%;
width: 100%;
font-family: Georgia;
}
</style>
</head>
<body>
<section class="slide">
<h1>This is the first div.</h1>
</section>
<section class="slide">
<p>This is the second div.</p>
</section>
<section class="slide">This is the third div.</section>
<section class="slide">This is the fourth div.</section>
<section class="slide">This is the fifth div.</section>
<section class="slide">This is the sixth div.</section>
<script type="text/javascript">
// Assign ids to each section in the order they appear.
$(document).ready(function(){
$("section").each(function(index){
idtag = 's' + (index + 1)
$(this).attr('id', idtag);
$(this).append('<button>Next div</button>');
$(this).css('opacity', 0);
});
var presentation = function() {
$("section").each(function(index){
$(this).css('opacity', 0);
});
// Check if the current url points to a specific id. If not point
// it to id = 1, otherwise point it to the id specified in the URL.
var currenturl = $(location).attr('href');
var indexhash = currenturl.lastIndexOf('#')
if (indexhash === -1) {
var newurl = currenturl + '#s1';
$("#1").css('opacity',1);
window.location.href = newurl;
}
else {
var currentid = currenturl.substring(indexhash, currenturl.length);
console.log(currentid);
$(currentid).css('opacity', 1);
window.location.href = currenturl;
// window.location.assign(currenturl);
}
};
var nextdiv = function() {
currenturl = location.href;
currentid = location.hash;
console.log(currentid);
newidnum = parseInt(currentid.substring(currentid.indexOf('s')+1, currentid.length),10)+1;
newid = "#s" + newidnum;
newurl = currenturl.substring(0,currenturl.lastIndexOf('#'))+newid;
console.log(newurl);
window.location.href = newurl;
};
// $(document).ready(presentation);
presentation();
$(window).bind('hashchange', presentation);
$('button').bind('click', nextdiv);
});
</script>
</body>
</html>
使用jquery选择多个DIV标记
html{
溢出:隐藏;
}
身体{
背景颜色:rgb(250250250);
}
h1{
字体大小:48px;
颜色:rgb(90,90,90);
}
.幻灯片{
显示:块;
位置:绝对位置;
边框样式:实心;
边框宽度:1px;
顶部:0px;
左:0px;
填充:20px;
身高:95%;
宽度:100%;
字体系列:格鲁吉亚;
}
这是第一组。
这是第二部分。
这是第三组。
这是第四组。
这是第五组。
这是第六组。
//按显示顺序为每个部分分配ID。
$(文档).ready(函数(){
$(“节”)。每个功能(索引){
idtag='s'+(索引+1)
$(this.attr('id',idtag));
$(this.append('Next div');
$(this.css('opacity',0);
});
var表示=函数(){
$(“节”)。每个功能(索引){
$(this.css('opacity',0);
});
//检查当前url是否指向特定id。如果不指向
//将其指向id=1,否则将其指向URL中指定的id。
var currenturl=$(位置).attr('href');
var indexhash=currenturl.lastIndexOf(“#”)
如果(indexhash===-1){
var newurl=currenturl+'#s1';
$(“#1”).css('opacity',1);
window.location.href=newurl;
}
否则{
var currentid=currenturl.substring(indexhash,currenturl.length);
console.log(currentid);
$(currentid).css('opacity',1);
window.location.href=currenturl;
//window.location.assign(currenturl);
}
};
var nextiv=函数(){
currenturl=location.href;
currentid=location.hash;
console.log(currentid);
newidnum=parseInt(currentid.substring(currentid.indexOf('s'))+1,currentid.length),10)+1;
newid=“#s”+newidnum;
newurl=currenturl.substring(0,currenturl.lastIndexOf('#'))+newid;
console.log(newurl);
window.location.href=newurl;
};
//美元(文件)。准备就绪(演示文稿);
演示文稿();
$(窗口).bind('hashchange',表示法);
$('button').bind('click',nextdiv);
});
这与section元素中的HTML元素无关,而是与您将所有元素堆叠在一起,并将不透明度设置为零,而不是使用display属性实际隐藏它们有关(display:none
vsopacity:0
)。如果删除除第一个部分元素之外的所有元素,则可以看到这一点,因为它工作正常。此外,如果您使用诸如Chrome的开发者工具之类的工具检查代码,您将看到第六部分元素实际上是最上面的元素,并且阻止了它下面的所有内容。您还可以每次单击相同的顶部按钮,而不是作为section元素子元素的按钮。通过为每个按钮分配一个id,然后控制台使用$('button')记录它,可以看到这一点。单击(function(){console.log($('button')).attr('id');})
这与section元素中的HTML元素无关,它与您将所有元素堆叠在一起并将不透明度设置为零这一事实有关,而不是使用display属性实际隐藏它们(display:none
vsopacity:0
)。如果删除除第一个部分元素之外的所有元素,则可以看到这一点,因为它工作正常。此外,如果您使用诸如Chrome的开发者工具之类的工具检查代码,您将看到第六部分元素实际上是最上面的元素,并且阻止了它下面的所有内容。您还可以每次单击相同的顶部按钮,而不是作为section元素子元素的按钮。通过为每个按钮分配一个id,然后控制台使用$('button')记录它,可以看到这一点。单击(function(){console.log($('button')).attr('id');})
为什么要使用节而不是div?看起来整个脚本都应该在$(文档)中。准备好了(…)
,而不仅仅是演示文稿
。是的,我在$(function(){…})的jquery表单中没有看到任何内容@Fallerenreaper感谢@Mathletics他们知道的更多…:)为什么要使用节而不是div?看起来整个脚本应该在$(文档)中。就绪(…)
,而不仅仅是演示文稿。
。是的,我在$(function(){…})的jquery表单中没有看到任何内容@Fallerenreaper感谢@Mathletics他们知道的更多…:)非常感谢你。我在想是否有更好的方法来替代不透明性,我突然想到,我可能会遇到元素与其他元素重叠的问题。虽然我不认为这里发布的问题与此有关。奇怪的是,这个问题只出现在添加html标记元素之后,而不是在此之前。谢谢你的回答。现在很好,非常感谢。我想问是否有一个更好的替代不透明的方法,我突然想到我可能有