最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modifying innerHTML from 3rd Party Script - Stack Overflow

programmeradmin2浏览0评论

I have a 3rd party script that is importing a menu and I cannot edit this 3rd party script. It generates code like this:

<div id="wmenu-updated" style="display: block;">
  <small>Menu updated</small>
  <span innerhtml="19 hours ago"></span>
</div>

It should take 19 hours ago and display that as text inside the span but for whatever reason it doesn't work and the makers of the script are little help in fixing the error.

Is there a way, using jQuery, that once the page loads I can take the innerhtml and spit it out as text inside that span?

I have a 3rd party script that is importing a menu and I cannot edit this 3rd party script. It generates code like this:

<div id="wmenu-updated" style="display: block;">
  <small>Menu updated</small>
  <span innerhtml="19 hours ago"></span>
</div>

It should take 19 hours ago and display that as text inside the span but for whatever reason it doesn't work and the makers of the script are little help in fixing the error.

Is there a way, using jQuery, that once the page loads I can take the innerhtml and spit it out as text inside that span?

Share Improve this question asked Feb 11, 2013 at 4:48 L84L84 46.3k59 gold badges181 silver badges259 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 6
$( document ).ready( function () {
    $( '#wmenu-updated span' ).text( function () {
        return $( this ).attr( 'innerhtml' );
    });
});

You can use jQuery's text function to update the text in the span

Use $.attr() and $.text() to achieve this

$(document).ready(function(){
   var txt = $("#wmenu-updated span").attr("innerhtml");
   $("#wmenu-updated span").text(txt);
});

Try this:

$(document).ready( function ( e ) {
    var q = $('span').attr('innerhtml');
    $('#wmenu-updated').children('span').text( q );
});

You can try this one: fiddle

 $(function(){
    $(document).children('#wmenu-updated').find('span[innerhtml]')
    .text($(this).attr("innerhtml"));
 });

or more simple like this:

$('span[innerhtml]').text($('span[innerhtml]').attr('innerhtml'));

Lots of people are getting it close :)

This will do it for you:

$('selector').find('span[innerhtml]').each(function() {
    $(this).html($(this).attr('innerhtml'));
});

jsFiddle here.

And this is functionally equivalent:

$('selector').find('span[innerhtml]').html(function() {
    return $(this).attr('innerhtml');
});
$("#wmenu span").attr("innerhtml", "Your Update here");
发布评论

评论列表(0)

  1. 暂无评论