javascript - 向网页添加 javascript 函数

标签 javascript

我一直在制作一些简单的 chrome 扩展,一切都很顺利,使用一些 javascript 在网页上编辑 html 非常简单。现在,当我想添加一些 JavaScript 函数时,问题就开始了,例如切换某个 div 等。这是我的代码

{
"name":"Warmane",
"description":"Some modifications to warmane forum.",
"version":"1.2",
"manifest_version":2,

"content_scripts": [
    {
      "matches": ["http://forum.warmane.com/*"],
      "js": ["myscript.js"]

    }
  ]
}

内容脚本

//Creating Elements

//Adding lates post button  
var nodeLi = document.createElement("LI");  
nodeLi.setAttribute("id", "latest_news");
var nodeA = document.createElement("A");               
var content = document.createTextNode("Latest posts");         
nodeA.appendChild(content);  
nodeA.setAttribute("class", "navtab");   
nodeA.setAttribute("href", "http://forum.warmane.com/search.php?do=getnew");    

//Adding mod cp button  
var nodeLiTwo = document.createElement("LI");  
nodeLiTwo.setAttribute("id", "mod_cp");
var nodeATwo = document.createElement("A");               
var contentTwo = document.createTextNode("Mod Cp");         
nodeATwo.appendChild(contentTwo);  
nodeATwo.setAttribute("class", "navtab");   
nodeATwo.setAttribute("href", "http://forum.warmane.com/modcp/"); 
//latest posts               
document.getElementById("navtabs").appendChild(nodeLi);     
document.getElementById("latest_news").appendChild(nodeA); 
//mod cp
document.getElementById("navtabs").appendChild(nodeLiTwo);     
document.getElementById("mod_cp").appendChild(nodeATwo);  

//Editing style 
document.getElementById("page-frame").setAttribute("style", "display: none");
document.getElementsByClassName("above_body")[0].setAttribute("style", "top: 170px");
document.getElementsByClassName("navigation-wrapper")[0].setAttribute("style", "top: 90px");

现在在这个网页上我想编辑我有一个div

<p class="toggle">Toggle</p>
<div id="someDiv"></div>  

每次我点击它时我想显示/隐藏<p> ,我不知道该怎么做。谁能向我解释一下我该怎么做?

最佳答案

这是一个 fiddle :http://jsfiddle.net/4qqxr1dx/

document.getElementsByClassName("toggle")[0].addEventListener("click",    function() { 
var displayed = document.getElementById("someDiv").style.display;
(displayed == "none") ? document.getElementById("someDiv").style.display = "inline" : 
document.getElementById("someDiv").style.display = "none";

});

我向第一个具有“toggle”类的 DOM 元素添加了一个事件监听器。 当您单击它时,它会检查 id 为“someDiv”的元素的显示属性

它将其放入三元运算中(更多信息在这里 -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator )

该操作确定是否显示,然后根据“display”属性的当前声明隐藏或显示。

关于javascript - 向网页添加 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334228/

相关文章:

javascript - ReactJS/ES6 : Searching Japanese text using includes?

javascript - 如何从异步调用返回响应?

javascript - Highcharts : Add xaxis dynamically doesn't consider what type I give it

javascript - 使用 Javascript 通过 if 语句更改样式表?

javascript - 拖放网格并具有推送效果

javascript - nock:如何模拟带有附加 header 的请求?我得到诺克:请求不匹配

Javascript 将字符串动态附加到脚本元素

javascript - 将多个输入值作为不同行插入不同表中

javascript - 如何将 Action 创建者绑定(bind)到 redux thunk 中的组件

javascript - JQuery:淡入/淡出效果