jQuery change the SRC and ID of an iFrame

2019-09-19 20:52发布

问题:

On my blog, I'm using a script that reveals anonymous messages as their normal username instead of anonymous. The script works fine on pages that I can make, but on pages that are default generated by my host (such as /ask, containing a message box which I cannot edit), I can't edit the code for the ask box so therefore it won't work. If anyone could provide a jQuery code that will

  1. Change the iFrame SRC with an ID of #askbox from "http://www.tumblr.com/ask_form/jamescharless.com" to "http://jamescharless.com/askbox"
  2. Remove the ID from the iFrame so it simply doesn't have an id

If anyone could help me it would be EXTREMELY helpful.

回答1:

This should change your IFrame src

$("#askbox").attr("src", "http://jamescharless.com/askbox");

You could try:

$("#askbox").removeAttr("id");

Combine the two:

$("#askbox").attr("src", "http://jamescharless.com/askbox").removeAttr("id");

Trying to solve your scripting problem...

Opening the page in a debugger you can run:

document.getElementById('wrapper');

but not

document.getElementById('ask_form') - it throws an error

Plan B

How about injecting a fresh IFrame?

$('#ask_form').parent().html('<iframe width="100%" height="149" 
    src="http://jamescharless.com/askbox" frameBorder="0" 
    scrolling="no" style="overflow: hidden; background-color: transparent;">
    </iframe>');


回答2:

   $(document).ready(function(){
       $('#askbox').attr("src", "http://jamescharless.com/askbox");
       $('#askbox').removeAttr("id");
   });