亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如何獲得元素的屬性

?我用下面的方法為什么不能獲得p3的屬性color,height,width??

function concel(){
???????? var p1=document.getElementById("p1");
???????? var p3=document.getElementById("p3");
???????? p1.style.color=p3.style.color.nodeValue;
???????? p1.style.width=p3.style.width.nodeValue;
???????? p1.style.height=p3.style.height.nodeValue;
???????? p1.style.display="block";
???? }

?

正在回答

2 回答

直接使用style屬性無法訪問到內嵌(寫在head標簽中的),外部(寫在css文件中的)的css樣式,只能訪問內聯樣式(寫在標簽中的)的css樣式

很用心在回答,滿意的話給個采納表示支持,感謝!


0 回復 有任何疑惑可以回復我~

可能有兩個原因導致你不能正確取消設置屬性的問題,導致你懷疑可能是沒有獲取到屬性。

  1. 你代碼里面是否設置了唯一的id

  2. 你獲取到的屬性已經被修改過,所以取消時再次賦值也取不到最初的值了。應該要保存最初屬性。

    貼一下驗證通過的代碼,可以參考下:


  3. var con=document.getElementById("con");

  4. var txt=document.getElementById("txt");

  5. var conColor=con.style.color;

  6. var conHeight=con.style.height;

  7. var conWidth=con.style.width;

  8. var txtIsShow=con.style.display;


  9. ? ? function changeColor(){

  10. ? ? ? ? con.style.color="pink";

  11. ? ? }


  12. //定義"改變寬高"的函數

  13. ? ? function changeWH(){

  14. ? ? ? ? con.style.height="500";

  15. ? ? ? ? con.style.width="300";


  16. ? ? }


  17. //定義"隱藏內容"的函數

  18. ? ? function hideTxt(){

  19. ? ? ? ? txt.style.display="none";

  20. ? ? }


  21. //定義"顯示內容"的函數

  22. function showTxt(){

  23. ? ? txt.style.display="block";

  24. }


  25. //定義"取消設置"的函數

  26. function cancel(){

  27. ? ? ?con.style.color=conColor;

  28. ? ? ? con.style.height=conHeight;

  29. ? ? ? ? con.style.width=conWidth;

  30. ? ? ? ? ? txt.style.display=txtIsShow;

  31. }

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

如何獲得元素的屬性

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號