JQuery replace html element contents if ID begins with prefix

I am looking to move or copy the contents of an HTML element. This has been asked before and I can get innerHTML() or Jquery's html() method to work, but I am trying to automate it. If an element's ID begins with 'rep_', replace the contents of the element after the underscore. So,
Hello World.
would replace:
Hrm it doesn't seem to work..
​ I've tried: $(document).ready(function() { $('[id^="rep_"]').html(function() { $(this).replaceAll($(this).replace('rep_', '')); }); });​ -and- $(document).ready(function() { $('[id^="rep_"]').each(function() { $(this).replace('rep_', '').html($(this)); }); ​});​ Neither seem to work, however, this does work, only manual: var target = document.getElementById('rep_target').innerHTML; document.getElementById('target').innerHTML = target; Related, but this is only text. https://stackoverflow.com/questions/4239444/jquery-replace-all-text-for-element-containing-string-in-id
where does the replacement come from? do you want to transfer-replace (transfer an existing content to replace target)? or clone-replace (copy an existing content and replace target)?

以上就是JQuery replace html element contents if ID begins with prefix的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准