如何在HTML div的innerHTML部分中添加按钮?

时间:2014-10-29 03:33:10

标签: javascript jquery html innerhtml

我在JavaScript& JQuery,更了解html& css但我有几天我一直在努力工作,把头撞到桌子上,无法弄明白。也许有人可以帮助我,任何提示,建议将不胜感激!

我有以下HTML:

<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内)添加另一个按钮,将您重定向到特定页面。我可以在HTML中的图像DIV中添加常规按钮,但是当它加载页面时默认情况下该按钮出现在该页面上,尽管它只在单击第一个按钮时显示(并打开innerHTML)。这意味着我必须以某种方式在JS中添加它。如果我按下关闭并重置div,则只有在单击第一个OPEN按钮时按钮才会显示。我不知道什么是JS脚本,以防止首先加载按钮并仅在我点击JS中的第一个按钮时加载它。

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

1 个答案:

答案 0 :(得分: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:我更新了fiddle以纳入我刚才提到的更改,似乎工作正常。同样,我知道Jquery语法不合适,但它是故意的。无论如何,我希望它能让你开始朝着正确的方向前进。此外,当使用javascript和Jquery时,坚持使用纯javascript或jquery,因为你可以在jquery中实现几乎所有你需要的功能,并且 更通用且易于使用。

相关问题