2017-07-07 22 views
0

私のコードは以下の通りです。iframeとアラート内のjQueryターゲットID

<!DOCTYPE html> 
<html> 

    <head> 
    <title>TODO</title> 
    </head> 

    <body> 
    <div id="btn"> 
     <button id="clickme">Click Me</button> 
    </div> 
    <div class="iframebox"> 
     <iframe id="messageiframe"> 
     <html> 

     <head> 
      <title>test</title> 
     </head> 

     <body> 
      <div id="textpreview"> 
      <p id="pre">some text</p> 
      </div> 
     </body> 

     </html> 
     </iframe> 
    </div> 
    </body> 

</html> 

私はjQueryのを使用してのiframe内id="textpreview"をターゲットにしようとしています。 iframe外のIDは正常にターゲティングできますが、iframe内はターゲティングできません。

私のjQueryのコードは次のとおりです。

$('#clickme').on('click', function() { 
    if ($("#textpreview").length > 0) { 
    alert("success"); 
    } 
}); 

私はこれをどのように達成することができますか?

ありがとうございます。

+0

jqueryプラグインはどこですか? –

答えて

0

<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <title>TODO</title> 
 
    </head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <body> 
 
    <div id="btn"> 
 
     <button id="clickme">Click Me</button> 
 
    </div> 
 
    <div class="iframebox"> 
 
     <iframe id="messageiframe"> 
 
     <html> 
 

 
     <head> 
 
      <title>test</title> 
 
     </head> 
 

 
     <body> 
 
      <div id="textpreview"> 
 
      <p id="pre">some text</p> 
 
      </div> 
 
     </body> 
 

 
     </html> 
 
     </iframe> 
 
    </div> 
 
    </body> 
 
<script> 
 

 
$('#clickme').on('click', function() { 
 

 
    if ($("#textpreview").has("pre")) { 
 
    alert("success"); 
 
    } 
 
}); 
 
</script> 
 
</html>

それはIDを持っているかどうかを確認してください。

関連する問題