简体   繁体   English

在任意页面的左侧添加div

[英]Add a div on the left side of a any page

I developing a plugin and want to add on any page a div on the left side, like a console. 我正在开发一个插件,想在任何页面的左侧添加div,例如控制台。 I saw CSS styles, but testing the plugin on greasymonkey not always show me the div, how can i do? 我看到了CSS样式,但是在greasymonkey上测试插件并不总是向我显示div,我该怎么办?

The CSS code that I'm using is this: 我正在使用的CSS代码是这样的:

var div_console = document.createElement("div");
div_consola.id = "div_consola";
div_consola.style.cssText = "overflow:scroll;
z-index:300;position:fixed;left:0px; width: auto; 
height: 100%;  border: solid 1px #e1e1e1;
vertical-align: middle;   background:  #ffdab9;text-align: center;";
var body = document.getElementsByTagName('body')[0];
body.appendChild(div_consola);

So, when loading any page I add with Javascript this div and then populate with data. 因此,在加载任何页面时,我都会在该div中添加Javascript,然后填充数据。

Any help? 有什么帮助吗?

Thank you!! 谢谢!!

div_consola.id = "div_consola"; div_consola.id =“ div_consola”;

div.setAttribute("id", "div_consola"); div.setAttribute(“ id”,“ div_consola”); <------ Try using setAttribute to set Id. <------尝试使用setAttribute设置ID。

EDIT: Using same Id name and variable name is also one of the issue. 编辑:使用相同的ID名称和变量名称也是问题之一。 When I changed the variable name in your code, it worked fine. 当我在代码中更改变量名称时,它可以正常工作。

var div_consola = document.createElement("div"); var div_consola = document.createElement(“ div”); var div = document.createElement("div"); var div = document.createElement(“ div”); <------ variable name changed <------变量名称已更改

A couple of things: 有两件事:

  1. You have typos in the variable name. 您在变量名称中有错别字。 I assume you mean div_console and not div_consola 我假设你的意思是div_console而不是div_consola
  2. You are assuming that a z-index of 300 will suffice, which may or may not be true. 您假设z索引为300就足够了,这可能是正确的,也可能不是正确的。 A page could choose to implement a z-index of 301. 页面可以选择实现301的z索引。

Is the rest of your css being applied to the div you wish to affect? 是否将其余的CSS应用于您希望影响的div?

You have specified 您已指定

overflow: scroll;
width: auto;

Depend of your browser, maybe your div have an undefined size and the scrollbar which pop hide your div. 取决于您的浏览器,也许您的div的大小不确定,弹出的滚动条会隐藏您的div。 That's what happened to me when I tried your code on jsFiddle. 这就是我在jsFiddle上尝试您的代码时发生的事情。

Here is the result when I change it to 这是我将其更改为

overflow : hidden;
width: 50px;

JsFiddle JsFiddle

Edit : If it doesn't resolve your problem, could you please precise the conditions (browser, etc) when it doesn't work ? 编辑:如果它不能解决您的问题,请问当它不起作用时,请调整条件(浏览器等)?

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

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