使用javascript点击iframe无法正常工作

时间:2016-06-15 15:16:13

标签: javascript jquery html css iframe

我有这个javascript代码:

function start1(dis,www){
    var visina = screen.availHeight;
    var content = '<a href="#" id="showRtb"><div id="showRtbn" style="position: fixed;text-align:center;left: 0px;width: 225px;height: 80px;top: 50%;z-index: 9999999;background: #E81515;cursor: pointer; border-radius:5px; margin-left:-15px;"> <iframe src="https://domain.com/hotel/2" style="border:0px #FFFFFF none;" name="myiFrame1" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe></div>   </a>          <div id="rtb" style="width:100%; height:100%; opacity:0; position:fixed; box-sizing: border-box; left:0px; top:0px; z-index:99999999; display:none; background:rgba(0, 0, 0, 0.8)"><iframe src="'+www+'" style="border:0px #FFFFFF none;" name="myiFrame" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe><div id="hideRtb" style="background:#fff; cursor:pointer; top:15px; right:15px;  z-index:9999999; position:fixed; border-radius:25px;"><img style="width:50px; height50px;" src="http://i.imgur.com/QqlcQwu.png"></div></div>';

var newNode = document.createElement("DIV");  
newNode.innerHTML = content;
document.body.appendChild(newNode); 



   var rtb = document.getElementById("rtb"),
  timer = null;

document.getElementById("showRtb").addEventListener("click", function() {
  if (rtb.style.opacity != 1) {
    clearTimeout(timer);
    rtb.style.display = "block";
    timer = setInterval(function() {
      rtb.style.opacity = +rtb.style.opacity + .10;
      if (+getComputedStyle(rtb).getPropertyValue("opacity") >= 1) {
        clearInterval(timer);
      }
    }, 30)
  }
});

document.getElementById("hideRtb").addEventListener("click", function() {
  if (rtb.style.opacity != 0) {
    clearTimeout(timer);
    timer = setInterval(function() {
      rtb.style.opacity = +rtb.style.opacity - .10;
      if (+getComputedStyle(rtb).getPropertyValue("opacity") <= 0) {
        rtb.style.display = "none";
        clearInterval(timer);
      }
    }, 30)
  }
});




}

正如您所看到的,我在身体内部创建了一个ID为showRtb的链接,并在内部创建了一个包含内容的iframe,但是当我点击它时它不起作用,并不想要显示ID为rtb

当我更改内容而不是iframe时,我会发布图片,然后效果很好,但现在使用iframe它不起作用。为什么?有什么问题?

所以,我想点击showRtb打开ID为rtb的div ...

更新:

var content = '<div id="showRtb1" style="position: fixed;text-align:center;left: 0px;width: 225px;height: 80px;top: 50%;z-index: 9999999;cursor: pointer; border-radius:5px; margin-left:-15px;"><iframe src="https://domain.com/hotel/2" name="myiFrame1" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe></div><a href="#" id="showRtb"><div id="showRtb" style="position: fixed;text-align:center;left: 0px;width: 225px;height: 80px;top: 50%;z-index: 99999999;cursor: pointer; opacity:0; pointer; border-radius:5px; margin-left:-15px;"></div></a><div id="rtb"><iframe src="'+www+'"  name="myiFrame" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe><div id="hideRtb"><img src="http://i.imgur.com/QqlcQwu.png"></div></div>';

1 个答案:

答案 0 :(得分:1)

改变这个:

var content = '<a href="#" id="showRtb"><div id="showRtbn"> <iframe src="https://domain.com/hotel/2" name="myiFrame1" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe></div></a><div id="rtb"><iframe src="'+www+'"  name="myiFrame" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe><div id="hideRtb"><img src="http://i.imgur.com/QqlcQwu.png"></div></div>';

类似于:

var content = '<div id="showRtbn"><iframe src="https://domain.com/hotel/2" name="myiFrame1" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe></div><a href="#" id="showRtb">Show RTB</a><div id="rtb"><iframe src="'+www+'"  name="myiFrame" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe><div id="hideRtb"><img src="http://i.imgur.com/QqlcQwu.png"></div></div>';

从锚标记(<a>..</a>)中删除iFrame,然后将其包含的<div>设置为隐藏。 注意:我删除了你的风格。您应该考虑将它们移到样式表中

JS:

document.getElementById("showRtb").addEventListener("click", function() {
  rtb.style.display = "block";
  console.log("clicked");
});