[英]Adding elements using browser console
I'm trying to add an element in a webpage using the browser console. 我正在尝试使用浏览器控制台在网页中添加元素。 My element is something like: 我的元素是这样的:
<a class="myclass" role="myrole" href="/url.com">
<span class="Label">Hello World</span>
</a>
How can I do this ? 我怎样才能做到这一点 ?
document.getElementById('myid').innerHTML = "<a class='myclass' role='myrole' href='/url.com'><span class='Label'>Hello World</span></a>";
You can add a dummy div or tag with some ID and use this code 您可以添加具有某些ID的虚拟div或标签,然后使用此代码
HTML: HTML:
<div id="myid">
</div>
function addElem(elem, text)
{
var elem = document.createElement(elem), // element to be created
text = document.createTextNode(text); // text node
bd = document.body; // get body
elem.appendChild(text); // elem appended with text
bd.appendChild(elem); // body appended with elem
return elem;
}
call it as so: addElem('p', 'text here');
这样称呼: addElem('p', 'text here');
call from console and see :) 从控制台调用并查看:)
try here itself 自己尝试一下
open your console, 打开控制台,
var _el = '<a class="myclass" role="myrole" href="/url.com"><span class="Label">Hello World</span></a>'
; var _el = '<a class="myclass" role="myrole" href="/url.com"><span class="Label">Hello World</span></a>'
;
$('.post-text').append(_el); $( '后置文本')追加(_el);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.