这就是我想要做的。
首先,这是在 Chrome 扩展程序中。
在 popup.js 文件中,它运行在 popup.html 上(这段代码是设置值,当我选择一个选择选项时):
function mySelectValue() {
// Add an event listener for the value
document.getElementById('mySelectValue').addEventListener('change', function() {
// Get the value of the name field.
var mySelectValue = document.getElementById('mySelectValue').value;
// Save the name in localStorage.
localStorage.setItem('mySelectValue', mySelectValue);
});
}
Run Code Online (Sandbox Code Playgroud)
现在之后我有代码来检索现在应该存储在 localstorage 中的值:
function getAndDisplayTheValue() {
myValue = localStorage.getItem('mySelectValue');
document.write(myValue);
}
Run Code Online (Sandbox Code Playgroud)
好的,这就是 javascript 文件。现在这里是 popup.html 文件:
<select id="mySelectValue">
<option name="" value="">choose a value</option>
<option value="first" name="first">first value</option>
<option value="second" name="second">second value</option>
<option value="third" name="third">third value</option> …Run Code Online (Sandbox Code Playgroud) 我有一个div,我想成为两种尺寸之一.
我尝试了以下代码,但它不起作用.我需要帮助才能让它发挥作用.
这是jsbin:http://jsbin.com/oFIRawa/1
这是我到目前为止的代码:
page.html中
<div id="theDiv"> </div>
Run Code Online (Sandbox Code Playgroud)
style.css文件
#theDiv {
background: #000;
border: 2px solid #222;
width: 300px;
height: 400px;
margin: 50px auto 0 auto;
}
Run Code Online (Sandbox Code Playgroud)
的script.js
$(document).ready(function () {
// call the method one time
updateWindowSize();
// subscribe the method to future resize events
$(window).resize(updateWindowSize);
// variables
var updateWindowSize = (function(){
var minAllowedWindowHeight = 500;
var largerDivHeight = 400;
var smallerDivHeight = 300;
// actual updateWindowSize function
return function(){
var winHeight = $(window).height();
var newHeight …Run Code Online (Sandbox Code Playgroud)