如何在父 html 文档中访问嵌入的 html 文档的子级

How to access embeded html document's children in parent html document

本文关键字:文档 html 访问      更新时间:2023-09-26

很简单,我想做的是使用 jquery 从中选择文本

<p class="oldtext">Hello world</p><p class="newtext"></p>

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="Ell" />
    <title>Trials</title>
    <script>
        $(document).ready(function(){
        }); //end ready
    </script>
</head>
<body>
<div>
    <p class="newtext"></p>
</div>
#document
<html>
    <head>
    </head>
    <body>
        <div>
            <p class="oldtext">Hello World</p>
        </div>
    </body>
</html>
</body>
</html>

请问我该怎么做?是否可以使用JQuery或Javascript?

像这样,请参阅小提琴:https://jsfiddle.net/5wwg1q5j/57/

<p class="oldtext">Hello World</p>
<p class="newtext"></p>
<script>
var str = $( ".oldtext" ).text();
$('.newtext').text(str);
</script>

你可能想要这样的东西:

$(function(){
  $('.newtext').load('yourOtherPageURL.html .oldtext', function(){
    // do stuff after load
  });
});

当然,很难确定你想对你不清楚的问题做什么。只是想帮忙。