提问者:小点点

如何在超文本标记语言div的innerHTML部分中添加按钮?


我对JavaScript很陌生

我有以下超文本标记语言:

<div id="projects" class="fluid">
<button onClick="myFunction2()" class="viewprojectsclose">Close</button>
<button onclick="myFunction()" class="viewprojects">View Projects</button>
<div id="projectsimg">
</div>
<div id="projectstxt">
</div>
</div>

使用以下JavaScript和JQuery:

function myFunction() {

document.getElementById("projectstxt").innerHTML = 'some text some text some text';
document.getElementById("projectstxt").style.borderBottom = "thin solid #000" ;     
document.getElementById("projectstxt").style.display = "block";  
document.getElementById("projectstxt").style.paddingBottom ="10px";     

         document.getElementById("projectsimg").style.padding = "100px" ;
         document.getElementById("projectsimg").style.background =     "url('../Assets/img/services/image1.png')" ;
         document.getElementById("projectsimg").style.backgroundRepeat = "no-repeat";
         document.getElementById("projectsimg").style.backgroundSize = "20%";
         document.getElementById("projectsimg").style.display = "block";
         document.getElementById("projectsimg").style.transition = "all 0.5s ease-in-out";
         document.getElementById("projectsimg").style.backgroundOrigin = "border-box";
         document.getElementById("projectsimg").style.marginTop = "5%";
;}  
function myFunction2() {
    document.getElementById("projectstxt").style.display = "none";
    document.getElementById("projectsimg").style.display = "none";
;}

$(document).ready(function()
  {
  $(".viewprojects").click(function(){
    $("#projects").animate({
        height:"300px"
        });
  });
  $(".viewprojectsclose").click(function(){
    $("#projects").animate({
        height:"80px"
        });
  });   
  });    

基本上我们有2个按钮(OPEN和CLOSE)。首先打开图像和文本DIV,第二个关闭。我想做的是在图像下方添加另一个按钮(在div中),它会将您重定向到特定页面。我可以在超文本标记语言的图像DIV中添加一个常规按钮,但是当它加载页面时,该按钮默认出现在它上面,尽管它应该只在单击第一个按钮时出现(并打开innerHTML)。这意味着我必须以某种方式在JS中添加它。如果我按关闭并重置div,那么按钮就会像它应该的那样出现,只有在单击第一个OPEN按钮时才会出现。我不知道什么是JS脚本,以防止加载按钮在第一和加载它只有当我点击第一个按钮在JS。

任何帮助都是神圣的!!提前谢谢你!


共1个答案

匿名用户

是的,有一个冲突,因为你在javascript和jquery中覆盖了按钮的click处理程序。你根本不必这样做。在MHO中,你应该坚持Jquery,只需在click处理程序中添加适当的DOM更改,如下所示:

$(document).ready(function()
{      
 $(".viewprojects").click(function(){

  //Animate first
   $("#projects").animate({
     height:"300px"
     });

 //Update DOM 
 document.getElementById("projectstxt").innerHTML = 'some text some text some text';
 document.getElementById("projectstxt").style.borderBottom = "thin solid #000" ;     
 document.getElementById("projectstxt").style.display = "block";  
 document.getElementById("projectstxt").style.paddingBottom ="10px";     

 document.getElementById("projectsimg").style.padding = "100px" ;
 document.getElementById("projectsimg").style.background =         "url('../Assets/img/services/image1.png')" ;
 document.getElementById("projectsimg").style.backgroundRepeat = "no-repeat";
 document.getElementById("projectsimg").style.backgroundSize = "20%";
 document.getElementById("projectsimg").style.display = "block";
 document.getElementById("projectsimg").style.transition = "all 0.5s ease-in-out";
 document.getElementById("projectsimg").style.backgroundOrigin = "border-box";
 document.getElementById("projectsimg").style.marginTop = "5%";
});


 $(".viewprojectsclose").click(function(){
  document.getElementById("projectstxt").style.display = "none";
  document.getElementById("projectsimg").style.display = "none";

  //Animate the div
  $("#projects").animate({
    height:"80px"
    });
 });   

 });    

现在,我刚刚重组了你在问题中发布的代码,让你了解如何修复它。代码没有经过清理,语法应该根据Jquery规范进行适当的更改,以简单易用。这是一个简单的练习,你可以自己做。

PS:我已经更新了小提琴,以纳入我刚才提到的更改,它似乎工作得很好。同样,我知道Jquery语法不合适,但它是故意的。无论如何,我希望它能让你朝着正确的方向开始。此外,当使用javascript和Jquery时,坚持使用纯javascript或jquery,因为你可以实现几乎所有你在jquery中需要的功能,它更通用且易于使用。