最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - InnerText returns undefined in content script Chrome extension - Stack Overflow

programmeradmin3浏览0评论

I'm trying to access HTML elements with content scripts, however I get undefined on innerText although using run_at: document_end in manifest.json.

My manifest.json:

{
  "manifest_version": 2,
  "name": "my extension",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab"
  ],
  "content_scripts": [
      {
        "matches": ["http://127.0.0.1:8000/*"],
        "js": ["jquery.js", "script.js"],
        "run_at": "document_end"
      }
  ]
}

script.js:

var address = $('.house span').innerText;                         
alert(address);

The webpage on http://127.0.0.1:8000 has a div with class house and a span element in it. The alert returns undefined, but when using it in the console it returns the actual innerText.

I'm trying to access HTML elements with content scripts, however I get undefined on innerText although using run_at: document_end in manifest.json.

My manifest.json:

{
  "manifest_version": 2,
  "name": "my extension",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab"
  ],
  "content_scripts": [
      {
        "matches": ["http://127.0.0.1:8000/*"],
        "js": ["jquery.js", "script.js"],
        "run_at": "document_end"
      }
  ]
}

script.js:

var address = $('.house span').innerText;                         
alert(address);

The webpage on http://127.0.0.1:8000 has a div with class house and a span element in it. The alert returns undefined, but when using it in the console it returns the actual innerText.

Share Improve this question asked Mar 23, 2016 at 15:05 ForgeForge 6,8547 gold badges48 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

innerText is not a property of jquery. use text or html method as per requirement

var address = $('.house span').text();   
var address = $('.house span').html();   
var address = $('.house span')[0].innerHTML;                         
发布评论

评论列表(0)

  1. 暂无评论