如何知道Javascript中输入的长度
alert("Size: "+document.getElementById('champ').length)
Run Code Online (Sandbox Code Playgroud)
不工作未定义
在我的Google Chrome扩展程序内容脚本中,我有以下内容:
jQuery(document).ready(function() {
var player = document.getElementById('player');
console.log(player);
console.log(document);
});
Run Code Online (Sandbox Code Playgroud)
我正在任何Hulu视频上运行它,其中嵌入了id ='player'
<embed id="player" type="application/x-shockwave-flash" src="/site-player/playerwrapper.swf?cb=e80c477cL" width="100%" height="428" style="z-index:10;" name="player" quality="high" allowscriptaccess="always" allowfullscreen="true" bgcolor="#000000" flashvars="bitrate=700000&user_id=-1&startLoadWrapperTime=1299572732904&overrideBrand=&referrer=http://www.hulu.com/watch/20338/saturday-night-live-steve-carell-monologue#continuous_play=on&continuous_play_on=true&sortBy=&initMode=4&modes=4&content_id=7751491&cb=2011-3-8-8&v=3">
Run Code Online (Sandbox Code Playgroud)
例如,以下链接http://www.hulu.com/watch/20337/saturday-night-live-snl-digital-short-the-japanese-office
但是当脚本运行时,console.log(播放器)返回null.但是,console.log(document)返回正确的文档对象.即使我尝试var player = Document.prototype.getElementById.apply(document, ['player']);
我仍然得到null.
有趣的是,如果我从Chrome JavaScript控制台尝试它,它工作正常,我得到适当的<embed>打印到控制台.
任何想法为什么这不适用于我的内容脚本?
更新:根据要求,以下是我的chrome扩展文件夹的完整内容:
manifest.json的:
{
"name": "gebidtest",
"version": "1",
"permissions": [
"http://*/*",
"https://*/*"
],
"content_scripts": [{
"matches": ["http://*/*"],
"js": ["jquery-1.5.1.min.js","app.js"]
}]
}
Run Code Online (Sandbox Code Playgroud)
app.js:
jQuery(document).ready( function() {
var player = document.getElementById('player');
console.log(player);
console.log(document);
});
Run Code Online (Sandbox Code Playgroud)
jquery-1.5.1.min.js:我刚刚下载了它
我在Chrome version 10.0.648.127Mac上使用,但我也在具有相同结果的PC上尝试过
javascript embed flash getelementbyid google-chrome-extension
我在这里有点困惑.我认为指定的函数在window.onload页面加载之前没有执行.尽管如此,我在下面的代码中得到一个错误(继承人的jsfiddle版本):
<script>
function updateImage(url){
document.getElementById("foo").src = url;
}
window.onload = updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");
</script>
<img id="foo" alt="" src="http://dummyimage.com/100x100/000/fff.png&text=bar" />
Run Code Online (Sandbox Code Playgroud)
它给了我:
Error: document.getElementById("foo") is null
Run Code Online (Sandbox Code Playgroud)
将图像移到脚本上方时,一切正常.
我有这个代码和身体
<body onload="mail_receive()">
Run Code Online (Sandbox Code Playgroud)
不会执行它.
function mail_receive() {
document.getElementById("w")="mail receive";
}
Run Code Online (Sandbox Code Playgroud)
它不会显示出来
<div id="w"></div>
Run Code Online (Sandbox Code Playgroud) 我需要在很长一段时间内第一次使用纯Javascript,并且已经习惯了jQuery的舒适床垫,所有重要的东西都在逃避我.
我需要在正则表达式上选择一堆div.所以我有这样的东西;
<div id="id_123456_7890123"> .. </div>
<div id="id_123456_1120092"> .. </div>
<div id="id_555222_1200192"> .. </div>
<div id="id_123456_9882311"> .. </div>
Run Code Online (Sandbox Code Playgroud)
而且我需要创建一个循环,遍历所有以id开头的div id_123456_.我该怎么做呢?
我以前使用jQuery和:regex过滤器插件,但看着它,似乎没有太多我可以在纯javascript重写中挽救.
一个document.getElementById回归的案例null.我在SO中读过其他四个问题,并阅读MDN上的参考资料,但我不知道出了什么问题; 请帮我.代码如下:
HTML
<button id="btnButton1">Button1!</button><br>
<button id="btnButton2">Button2!</button><br>
<span id="spanOutPut"></span>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
getBYid = function(elem) {
return document.getElementById(elem); }
funButton1 = function() { getBYid('spanOutPut').innerHTML = "Button 1 pressed!!" };
funButton2 = function() { getBYid('spanOutPut').innerHTML = "Did you press the Button 2?!" };
getBYid("btnButton1").addEventListener('click', funButton1, false);
getBYid("btnButton2").addEventListener('click', funButton2, false);
Run Code Online (Sandbox Code Playgroud)
我TypeError: getBYid(...) is null在FireBug上得到了一个.
当我只是addEventListener从JS中删除调用并设置onclick内联时,它就可以工作,如下面的代码所示:
<button onclick="funButton1()">Button1"</button>
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我想知道当div有一定值时我怎么能让JavaScript做出反应.
<div id="background">
<div id="value">1</div>
</div>
<script>
var text = document.getElementById("value");
if (text = "0") {
document.getElementById("background").style.backgroundColor="red";
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果值div为0,我希望背景颜色为div的红色,称为背景.如果值div具有其他值,则它不应执行任何操作.这就是我所要做的.但它不起作用.即使我在值div中添加了另一个值,红色背景仍会显示.除非我清空了值div,然后当然你什么也看不见.
我想问你如何从JavaScript中的变量中获取id的元素.例如,我在JavaScript变量中包含html源代码,我想从这个变量中获取元素.
例如:
var html = "<html><body><div id='test'>Hi</div> ... lot of code ... <div id='test'>Hi</div><div id='test'>Hi</div> ... lot of code ... </body></html>";
var get = html.getElementsById("test");
Run Code Online (Sandbox Code Playgroud)
但它不起作用
非常感谢你的帮助
我有这个脚本,应该在单击按钮时更改按钮的文本.
<body>
<button onclick="toggleText(this);" id="id">Edit</button>
</body>
function toggleText(element){
var text = document.getElementById(element.id).textContent;
if (text == 'Edit') {
text = 'Done';
} else {
text = 'Edit';
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.它只适用于document.getElementById(element.id).textContent直接放入if语句的情况.
如何正确存储变量?
希望是一个简单的,但我无法从for循环打印出一个数字系列,回到HTML.看下面我的代码,当我点击按钮时,它只返回10,但不是其他数字 - 我哪里错了?
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try It</button>
<p id="number"></p>
<script>
function myFunction() {
for (i=0; i<11; i++) {
document.getElementById("number").innerHTML=i+"</br>";
}
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)