我真正需要的是像 Firebug 中的 hover-element 这样的功能,我们可以在其中获取选中的 HTML。
在我们的程序中,我们将加载一个 URL,该 URL 将在 iFrame 中打开。这里我们想要悬停元素,当用户点击某个元素时,我们需要该特定元素的 HTML 源代码。
所以:
- 您能否与 iFrame 交互(获取悬停的 html),以便获取选定的 HTML?
最佳答案
这是您想要的东西。它将在一个名为 sourcecode 的 div 中运行。
但是,如果需要,您可以将源代码 div 放在 iframe 中。
<div class="sourceCode">
</div>
<script type="text/javascript">
(function ($) {
$.fn.outerHTML = function () {
return $(this).clone().wrap('<div></div>').parent().html();
}
})(jQuery);
$(document).ready(function () {
$("p,div").click(function () {
var thisobject = this;
var sourcecodes = $(thisobject).outerHTML();
sourcecodes = htmlEscape(sourcecodes);
$(".sourceCode").html(sourcecodes);
});
});
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
</script>
关于javascript - 与 iframe 交互 - 获取选定的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11794876/