Display source html in iframe on rollover

两盒软妹~` 提交于 2019-12-13 05:06:04

问题


I have a db with one field containing the source html of webpages. I have a table that shows the url that leads to the source HTML.

<table>
    <tr>
        <td>This is a Td</td>
        <td>This is a Td</td>
        <td class="hover">URL1</td>
        <td>This is a Td</td>
    </tr>
    <tr>
        <td class="hover">URL1</td>
        <td>This is a Td</td>
        <td>This is a Td</td>        
        <td>This is a Td</td>
    </tr>
</table>
<div class="stuff">
</div>

when I roll over a url in the table , I want to display the associated db HTML in an iframe.

based on:

http://jsbin.com/urarem/3/edit?html,css,output

which appears to do exactly what I want, I have:

$(document).ready(function() {
    $('.hover').on('mouseover', function() {
        var html = '<a href="URL"></a><div class="box"><iframe src="URL" width = "500px" height = "500px"></iframe></div>';
        $('.stuff').html(html);
    });

Assuming I dynamically load the html source into the variable 'html' on rollover , how do I display this in an Iframe as above?


回答1:


You just get the value or innerHTML (depending on how the original HTML is laid out), and then load that into the src of the iframe. You can do it like this:

JS:

$('.hover').on('mouseover', function(e) {
    var url = $(this).html();
    $('.stuff').attr('src',url).show();
}).on('mouseleave', function(e) {
   $('.stuff').hide().removeAttr('src'); 
});

HTML:

<table>
    <tr>
        <td>This is a Td</td>
        <td>This is a Td</td>
        <td class="hover">www.wikipedia.com</td>
        <td>This is a Td</td>
    </tr>
    <tr>
        <td class="hover">www.google.com</td>
        <td>This is a Td</td>
        <td>This is a Td</td>        
        <td>This is a Td</td>
    </tr>
</table>
<iframe class="stuff">
</iframe>

JSFiddle example.



来源:https://stackoverflow.com/questions/28441391/display-source-html-in-iframe-on-rollover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!