Get the contents of a class inside an iframe, with

2019-08-06 09:44发布

I have a Greasemonkey script which uses jQuery and detects if a "class" is present in the document.

var damageMessage = $(".mw_error").text();

This works fine when the document loads and damageMessage holds a text string.

The problem is that the "class" .mw_error is inside an iframe and when you click a new link inside, the iframe content loads without a page refresh but the contents of "class" do change and I want to get the new contents or text string.

I’m guessing it’s because the document doesn’t reload that the Greasemonkey script doesn’t load again and try and find the updated class contents. Somehow I need to get the new contents of the class.

Any help on this would be much appreciated :)

1条回答
贼婆χ
2楼-- · 2019-08-06 10:26

Use the iframe on load method and get the text of the required element like this. This will ensure that every time iframe is refreshed you will get the new text that is loaded inside iframe.

$("iframe").load(function(){

   var damageMessage = $(this).contents().find(".mw_error").text();

});
查看更多
登录 后发表回答