2017-09-16 70 views
-1

我在react.js. 我得到这个错误初学者:无法读取的未定义的属性“的getAttribute” - reactjs

Cannot read property 'getAttribute' of undefined full error output in my browser chrome console:

Uncaught TypeError: Cannot read property 'getAttribute' of undefined at clickOnBlock (bundle.js:21172) at ActionLink (bundle.js:21232) at bundle.js:7557 at measureLifeCyclePerf (bundle.js:7327) at ReactCompositeComponentWrapper._constructComponentWithoutOwner (bundle.js:7556) at ReactCompositeComponentWrapper._constructComponent (bundle.js:7531) at ReactCompositeComponentWrapper.mountComponent (bundle.js:7439) at Object.mountComponent (bundle.js:13856) at ReactCompositeComponentWrapper.performInitialMount (bundle.js:7622) at ReactCompositeComponentWrapper.mountComponent (bundle.js:7509)

我的代码:

var React = require('react'); 
var ReactDOM = require('react-dom'); 
var x; 
var y; 
var z; 
var zz; 
var counter=0; 
function ActionLink() { 

    function clickOnBlock(d) { 
     if (counter<2){ 
      x = d.getAttribute("data-color"); 
      y = d.getAttribute("data-u"); 
      document.getElementById("d" + (y)).style.backgroundColor = x; 
      document.getElementById("lastClick").value = x; 

      if(counter==0) { 
       // Store 
       localStorage.setItem("keepLast", x); 
       // Retrieve 
       z= document.getElementById("result").innerHTML = localStorage.getItem("keepLast"); 
      } 

      if(counter==0) { 
       // Store 
       localStorage.setItem("id", y); 
       // Retrieve 
       zz= document.getElementById("resultId").innerHTML = localStorage.getItem("id"); 
      } 
      counter++; 

      if(counter==2){ 
       if(x==z && y!=zz){ 
        yes(); 
       }else{ 
        no(); 
       } 
      } 
     } 
    } 
    function no(){ 
     setTimeout(function() { 
      remove(); 
     }, 1000); 
    } 

    function remove(){ 
     document.getElementById("d" + (y)).style.backgroundColor =""; 
     document.getElementById("d" + (zz)).style.backgroundColor =""; 
     counter=0 

    } 

    function yes(){ 
     setTimeout(function() { 
      ok(); 
     }, 1000); 
    } 
    function ok() { 
     document.getElementById("d" + (y)).style.backgroundColor = ""; 
     document.getElementById("d" + (zz)).style.backgroundColor = ""; 
     document.getElementById("d" + (y)).style.backgroundColor = "black"; 
     document.getElementById("d" + (zz)).style.backgroundColor = "black"; 
     counter=0 
    } 


    return (
     <div> 
      <div id="bigdiv"> 
       <a id="d1" type="button" data-u="1" data-color="green" onClick={clickOnBlock(this)}></a> 
       <a id="d2" type="button" data-u="2" data-color="yellow" onClick={clickOnBlock(this)}></a> 
       <a id="d3" type="button" data-u="3" data-color="deeppink" onClick={clickOnBlock(this)}></a> 
       <a id="d4" type="button" data-u="4" data-color="green" onClick={clickOnBlock(this)}></a> 
       <br/> 
       <a id="d5" type="button" data-u="5" data-color="blue" onClick={clickOnBlock(this)}></a> 
       <a id="d6" type="button" data-u="6" data-color="orange" onClick={clickOnBlock(this)}></a> 
       <a id="d7" type="button" data-u="7" data-color="deeppink" onClick={clickOnBlock(this)}></a> 
       <a id="d8" type="button" data-u="8" data-color="red" onClick={clickOnBlock(this)}></a> 
       <br/> 
       <a id="d9" type="button" data-u="9" data-color="red" onClick={clickOnBlock(this)}></a> 
       <a id="d10" type="button" data-u="10" data-color="yellow" onClick={clickOnBlock(this)}></a> 
       <a id="d11" type="button" data-u="11" data-color="orange"onClick={clickOnBlock(this)}></a> 
       <a id="d12" type="button" data-u="12" data-color="blue" onClick={clickOnBlock(this)}></a> 
      </div> 
      <input id="lastClick" type="hidden" value="" /> 
       <div id="result" className="dd" ></div> 
       <div id="resultId" className="dd" ></div> 
     </div> 

    ); 
} 

ReactDOM.render(<ActionLink /> , document.getElementById('app')); 

回答

0

将事件传递到您的功能并获得属性e.target

<a id="d1" type="button" data-u="1" data-color="green" onClick={(e) => clickOnBlock(e)}></a> 

function clickOnBlock(e) { 
    console.log(e.target.getAttribute("data-color")); 
} 

注:

onClick={clickOnBlock(param)} 

此功能将与每一个渲染执行,无需点击。你需要传递一个函数onClick,但是你传递了这个函数的执行结果。为了避免这种情况,请使用箭头功能:

onClick={() => clickOnBlock(param)} 
0

当传递的事件处理程序你不不需要调用处理程序或将this作为目标。
尝试改变您从该通过该处理程序的方式:

`onClick={clickOnBlock(this)}` 

向该:

onClick={clickOnBlock} 
0
function clickOnBlock() { 
    return (d) => { if (counter<2){ 
     x = d.getAttribute("data-color"); 
     y = d.getAttribute("data-u"); 
     document.getElementById("d" + (y)).style.backgroundColor = x; 
     document.getElementById("lastClick").value = x; 

     if(counter==0) { 
      // Store 
      localStorage.setItem("keepLast", x); 
      // Retrieve 
      z= document.getElementById("result").innerHTML = localStorage.getItem("keepLast"); 
     } 

     if(counter==0) { 
      // Store 
      localStorage.setItem("id", y); 
      // Retrieve 
      zz= document.getElementById("resultId").innerHTML = localStorage.getItem("id"); 
     } 
     counter++; 

     if(counter==2){ 
      if(x==z && y!=zz){ 
       yes(); 
      }else{ 
       no(); 
      } 
     } 
    } 
}} 

的onClick = {()=> this.clickOnBlock()}

相关问题