简体   繁体   English

使用javascript书签将输入框和按钮添加到现有页面中

[英]Adding an input box and button into an existing page with javascript bookmarklet

Is it possible to have a javascript bookmarklet to add in a new input box into an existing page? 是否可以使用javascript小书签将新的输入框添加到现有页面中? Basically I need to have a prompt box to save a variable but I don't want it to pop up, I want it just in the page so I can then have it filled out and submit so the variable can be used in another piece of javascript. 基本上,我需要有一个提示框来保存变量,但我不希望它弹出,我只希望它在页面中,这样我就可以填写并提交,以便该变量可以在另一个变量中使用javascript。 Its kind of confusing, lol, but just wondering if this is possible. 这有点令人困惑,大声笑,但只是想知道这是否可能。

Thanks 谢谢

Yes, it's possible. 是的,有可能。

For example: 例如:

javascript:void(document.body.appendChild(document.createElement('input')));

would simply append an input-box to the document's body. 只需将输入框附加到文档主体即可。

The void() is needed because of appendChild() has a return-value, so if you dont use void, your location will be forwarded to that return-value. 之所以需要void()是因为appendChild()具有返回值,因此,如果您不使用void,您的位置将被转发到该返回值。

You can add attributes like you usually do in Javascript. 您可以像在Javascript中一样添加属性。 Save the created element into a variable, and then assign the attributes. 将创建的元素保存到变量中,然后分配属性。

An example, how to do this: 例如,如何执行此操作:

javascript:void((function(){var obj=document.body.appendChild(document.createElement('input'));obj.value='someValue';alert('It works, value is \n'+document.body.lastChild.value);})());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM