相关疑难解决方法(0)

类型错误:无法将属性“textContent”设置为 null

目前这段代码存在问题一段时间了,无论我检查多少次,我都会不断收到错误。

错误都是一样的:

类型错误:无法将属性“textContent”设置为 null

当我使用 querySelector 或当我使用 getElementById 时。我不知道这是我的 HTML 还是我输入错误。

我感到困惑的是它在这里工作......但是当我使用我的 VSC(Visual Studio 代码)并在 chrome 上运行它时,会弹出错误,错误显示。是我的代码还是VSC?

var dice = Math.floor(Math.random()* 6) +1;

document.querySelector("#current-0").textContent = dice;
Run Code Online (Sandbox Code Playgroud)
<div class="wrapper clear-fix">
    <div class="player1-panel active">
        <div class="player-name" id="player-1">Player 1</div>
        <div class="player-score" id="score-1">100</div>
        <div class="player-current-box">
            <div class="player-current-label">Current</div>
            <div class="player-current-score" id="current-0">11</div>
         </div>
    </div>

        <div class="player2-panel">
            <div class="player-name" id="player-2">Player 2</div>
            <div class="player-score" id="score-2">00</div>

            <div class="player-current-box">
                <div class="player-current-label">Current</div>
                <div class="player-current-score" id="currentScore-2">00</div>
            </div>
        </div>
        <button class="btn-rules"><i class="material-icons">
                help</i>Rules</button>
        <button class="btn-newGame"><i class="material-icons">
                add_circle_outline
            </i>New Game</button>
        <button class="btn-rollDice"><i class="material-icons">
                autorenew</i>Roll …
Run Code Online (Sandbox Code Playgroud)

html javascript dom-manipulation

5
推荐指数
1
解决办法
6万
查看次数

聚合查询选择器在DartVM上工作但在编译后不在Chrome中

有一个奇怪的问题.在我的Dart代码中,我在屏幕上有一些聚合物组件,其中一个有我从main()调用的方法.

我抓住它的参考

PolyComp poly = querySelector("#idOfPolymer");
poly.flash();
Run Code Online (Sandbox Code Playgroud)

这在飞镖中完美无缺.页面加载并且PolyComp开始闪烁.但是当我通过从Dart IDE运行Build Polymer应用程序在Chrome中运行它时,我收到一条错误,指出无法在null上调用flash().

我最后只是使用一个事件总线让PolyComp听我的事件,但是这太过分了.

我究竟做错了什么?这种情况发生在最新的Chrome,Firefox和Safari中.

编辑:

我还为JS构建了以下聚合物应用程序并遇到了同样的问题. https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/todo_element/todo.html

适用于DartVM,而不是Chrome,因为它在null元素上调用方法.

dart dart-polymer

4
推荐指数
1
解决办法
841
查看次数

当我在头部放置一个脚本元素以从URL加载它时,我的Javascript不起作用

我已经为内联的简单函数编写了代码,但是当我创建一个单独的js.file时,它不想因某些原因而工作.我尝试了一切感觉,但也许我疲惫的眼睛不能做什么!

<!DOCTYPE html>

<html>
  <head>
    <title>Test</title>
    <script type="text/javascript" src="menu.js"></script>
  </head>
 <body>
<div class="container">
  <button name="one">Button 1</button>
  <p>
    Lorem ipsum dolor sit amet
  </p>

</div>
<div class="container">
  <button name="two">Button 2</button>
  <p>
    Lorem ipsum dolor sit amet
  </p>

</div>
<div class="container">
  <button name="three">Button 3</button>
  <p>
    Lorem ipsum dolor sit amet
  </p>
</div>
Run Code Online (Sandbox Code Playgroud)

这个想法是有三个按钮,当你点击其中一个按钮时,只显示一个div,另外两个将被隐藏.

这是JavaScript(内联工作非常精细):

var first_container = document.querySelectorAll(' div:not(:first-child) p');

for (var i = 0; i < first_container.length; i++) {
  first_container[i].style.visibility = 'hidden';
}
var buttons = document.querySelectorAll('button');

for (var i = …
Run Code Online (Sandbox Code Playgroud)

html javascript

4
推荐指数
1
解决办法
2万
查看次数

Bootstrap模态事件未触发

在我的模态被解雇后,我想简单地做一些逻辑.

在模态中,我有一个按钮:

<button type="button" class="btn btn-primary" onclick="$('#mymodal').modal('hide');">Save changes</button>
Run Code Online (Sandbox Code Playgroud)

在视图中,我正在等待事件被触发,但它永远不会.

 $('#mymodal').on('hide.bs.modal', function (e) {
       alert('event fired')
});
Run Code Online (Sandbox Code Playgroud)

我试图在chrome dev工具中放置断点,事件只在视图的第一次加载时被击中.之后,再也不会达到断点.难道我做错了什么?

顺便说一句,模态隐藏了我想要的方式.

jquery modal-dialog twitter-bootstrap

4
推荐指数
2
解决办法
1万
查看次数

我收到一个未捕获的类型错误:无法设置 null 的属性“onclick”?

我收到未捕获的类型错误:未捕获的类型错误:无法设置 null 的属性“onclick”

<!DOCTYPE html>
<html>
    <head>
        <title>dice app</title>
        <script src="widget.js"></script>
    </head>
    <body>
        <button id="button">Roll Dice</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

js代码:

var button = document.getElementById("button");

button.onclick = function() {// error
    var print = dice.roll();
    printNumber(print);
};
Run Code Online (Sandbox Code Playgroud)

javascript dom

4
推荐指数
1
解决办法
3804
查看次数

为什么DOM元素为空?

我有这个简单的HTML文件:

<!DOCTYPE html>
<html>
<head>
    <script src='test.js'></script>
</head>

<body>
    <p>I am a paragraph tag</p>
    <h1 >I am an h1 tag</h1>
    <div id="id"> I am a div tag</div>
</body>
Run Code Online (Sandbox Code Playgroud)

这个简单的脚本(test.js):

y=document.getElementById("id");
y.style.color="green";
Run Code Online (Sandbox Code Playgroud)

为什么在地球上“ y”为空?我得到的错误是

TypeError: y is null
Run Code Online (Sandbox Code Playgroud)

我确定这是我所缺少的简单语法,但是我一生都无法解决!救命!

脚本发布:html文件和test.js文件都在同一文件夹中。

javascript styles runtime-error

4
推荐指数
1
解决办法
2629
查看次数

document.querySelector 总是返回 null

看了其他答案,还是没明白。

出于某种原因,这行代码总是返回 null。

var els = document.querySelector("[id='MTG_INSTR$2']");
Run Code Online (Sandbox Code Playgroud)

我在控制台中检查了 document 的值,所以我很确定这是正确的。

id 深埋在一张桌子里面,这可能是一个问题吗?

编辑 2:如果有帮助,这里是完整的代码。

内容.js

chrome.runtime.onMessage.addListener(
      function(request, sender, sendResponse) {
        if( request.message === "start" ) {
         start();
             }
      }
    );

    function start(){
        var els = document.querySelector("[id='MTG_INSTR$2']");
        console.log(els);
        alert("started");
    }
Run Code Online (Sandbox Code Playgroud)

弹出窗口.html

<!DOCTYPE html>
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
Run Code Online (Sandbox Code Playgroud)

弹出窗口.js

 function popup() {
    chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
   });
}

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("button1").addEventListener("click", popup);
});
Run Code Online (Sandbox Code Playgroud)

清单文件

{
"content_security_policy": …
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
2
解决办法
9331
查看次数

如果我将变量“close”全局定义为“0”,为什么它会被记录为“false”?

我知道这一定是非常基本的东西,但我不了解范围是如何工作的。我希望closed整个 JavaScript 文件都知道该变量。

\n\n

我有类似的东西(在 jQuery 中):

\n\n
var closed = 0;\n\n$(function(){\n  console.log(closed);\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

closed被记录为false. load我用和函数尝试了很多东西onload,但失败了。

\n

javascript variables scope

4
推荐指数
1
解决办法
517
查看次数

Html 5 canvas getElementById() 返回 null/未定义

这是代码:

HTML:

<body onload="initializeMap()">
    <div id="map_canvas" style="width:100%; height:100%; z-index:1"></div>
    <canvas id="control" style="width:100%; height:100%; z-index:2">Does Not Support Canvas Element</canvas>
</body>
Run Code Online (Sandbox Code Playgroud)

JavaScript:

<script type="text/javascript">
    var canvas = document.getElementById('control');
    var context = canvas.getContext('2d');

    function draw(){
        context.font = "bold 12px sans-serif";
        context.fillText("x", 248, 43);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

绘制函数是在谷歌地图初始化后调用的,所以 DOM 应该已经加载了,对吗?我可能做错了什么?

html html5-canvas

3
推荐指数
1
解决办法
2万
查看次数

如何遍历所有<p>元素?

我想循环遍历文档中的所有段落元素并调整它们的html.目前我使用一些jquery:

console.log("loop through <p> elements");    

$("p").each(function()
{
    console.log("next <p> element with id: " + this.id);
});
Run Code Online (Sandbox Code Playgroud)

然而,在控制台中只出现"循环段落元素",我的身体中有一些带有唯一ID的段落元素:

<body>
    <p id="para1"> paragraph 1 </p>
    <p id="para2"> paragraph 2 </p>     
    <p id="para3"> paragraph 3 </p> 
</body>
Run Code Online (Sandbox Code Playgroud)

我希望它与.each()循环有某种语法错误,有人可以纠正我,任何帮助都会非常感激.

html javascript each jquery loops

3
推荐指数
1
解决办法
4763
查看次数