使用chrome.tabs.executeScript检查一个框,无法正常工作

时间:2012-04-29 08:37:19

标签: javascript google-chrome-extension

我无法检查此页面中的复选框。

<html>
  <body>
    <form name="f">
      <input type="checkbox" name="vehicle" value="Bike" /> 1<br /></li>
    </form>
  </body>
</html>

我的popup.js如下:

function click(e) {
  chrome.tabs.executeScript(null,
      {code:"document.body.f.vehicle.checked='true'"});
  window.close();
}

document.addEventListener('DOMContentLoaded', function () {
  var divs = document.querySelectorAll('div');
  for (var i = 0; i < divs.length; i++) {
    divs[i].addEventListener('click', click);
  }
});

在控制台日志中我不断收到错误:未捕获的TypeError:无法读取未定义的属性“vehicle”。

我的popup.html如下:

<!doctype html>
<html>
  <head>
    <title>Set Page Color Popup</title>
    <style>
    body {
      overflow: hidden;
      margin: 0px;
      padding: 0px;
      background: white;
    }

    div:first-child {
      margin-top: 0px;
    }

    div {
      cursor: pointer;
      text-align: center;
      padding: 1px 3px;
      font-family: sans-serif;
      font-size: 0.8em;
      width: 100px;
      margin-top: 1px;
      background: #cccccc;
    }
    div:hover {
      background: #aaaaaa;
    }
    </style>
    <script src="popup.js"></script>
  </head>
  <body>
    <div id="1">1</div>
  </body>
</html>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

应该是这样的,

document.f.vehicle.checked='true';

http://jsfiddle.net/PnXsE/

相关问题