2016-06-15 4 views
0

У меня есть этот код: яваскрипт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) 
    } 
}); 




} 

и как вы можете видеть, я создаю ссылку внутри тела с идентификатором showRtb и внутри, что в IFRAME с содержанием, но когда Я не хочу показывать div с id rtb ...

Когда я меняю контент, поэтому вместо iframe я отправляю изображение, тогда он работает хорошо, но теперь с iframe он не работает. Зачем? В чем проблема?

Итак, я хочу, когда я нажимаю на showRtb, чтобы открыть DIV с ID RTB ...

UPDATE:

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

У вас нет доступа к плавающему фрейму с содержимым из другого сервера. Кроме того, iFrame не имеет доступа к родительскому или скрипту в родительском. Также ваш HTML недопустим. – mplungjan

+0

Нет, я не хочу доступа, я просто хочу, когда нажимаю на этот идентификатор iframe, чтобы открыть другой div с ID rtb ... –

+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>'; 

Извлеките iFrame из анкерных меток (<a>..</a>), и вы можете установить его содержащий <div> как скрытый. ПРИМЕЧАНИЕ: Я удалил ваши стили. Вы должны рассмотреть вопрос о переносе их в таблицу стилей

JS:

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

вообще не показывать .... –

+0

что не отображается? –

+0

Я не вижу iframe и div, ничего ... –

Смежные вопросы