HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素相关的知识,希望对你有一定的参考价值。

/**
* THIS FUNCTION WILL RENDER A CONTAINER OR SELF-CLOSING html ELEMENT.
* IT AUTO DETECTS WHAT TYPE YOU WANT
* BASED ON RATHER OR NOT YOU SET A _data KEY IN THE ARRAY OF
* ATTRIBUTES PASSED TO THE FUNCTION
*
* USING AN ASSOCIATIVE ARRAY ATTRIBUTES ARE ASSIGNED TO THE ELEMENT
*
* IF THE KEY _data IS SET IN THE $propBag ARRAY
* _data WILL BE INSERTED BETWEEN THE START AND END TAGS
* AS THE ELEMENT IS NOT SELF CLOSING BECAUSE IT HAS DATA TO CONTAIN.
*
* IF _data IS NOT SENT IN THE ARRAY THE TAG WILL BE FORMED AS SELF CLOSING
* BECAUSE IT HAS NO DATA TO CONTAIN
*/
SEE Examples Of Use Below The Code
  1. /**
  2.  * THIS FUNCTION WILL RENDER A CONTAINER OR SELF-CLOSING HTML ELEMENT.
  3.  * IT AUTO DETECTS WHAT TYPE YOU WANT
  4.  * BASED ON RATHER OR NOT YOU SET A _data KEY IN THE ARRAY OF
  5.  * ATTRIBUTES PASSED TO THE FUNCTION
  6.  *
  7.  * USING AN ASSOCIATIVE ARRAY ATTRIBUTES ARE ASSIGNED TO THE ELEMENT
  8.  *
  9.  * IF THE KEY _data IS SET IN THE $propBag ARRAY
  10.  * _data WILL BE INSERTED BETWEEN THE START AND END TAGS
  11.  * AS THE ELEMENT IS NOT SELF CLOSING BECAUSE IT HAS DATA TO CONTAIN.
  12.  *
  13.  * IF _data IS NOT SENT IN THE ARRAY THE TAG WILL BE FORMED AS SELF CLOSING
  14.  * BECAUSE IT HAS NO DATA TO CONTAIN
  15.  */
  16. /**
  17.  * html()
  18.  *
  19.  * @param mixed $elm
  20.  * @param mixed $propBag
  21.  * @return
  22.  */
  23. function html($elm,$propBag){
  24. $self_closing=false;
  25. $attributes="";
  26. $contained_data=""; //THE VALUE OF THE '_data' ARRAY ELEMENT IN $propBag
  27. if(array_key_exists("_data",$propBag)){ //IF _data EXISTS IN $propBag
  28. $contained_data = $propBag['_data']; //SAVE THE VALUE IN A VARIABLE
  29. unset($propBag['_data']); //REMOVE contained_data FROM THE ARRAY
  30. $self_closing=false; //indicate this is not a self closing tag because we have data to contain
  31. }else{ //IF contained_data IS NOT A KEY IN THE ARRAY
  32. $contained_data = NULL;//SET THE $contained_data VARIAB;E TO NULL
  33. $self_closing=true; //indicate it is a self-closing tag that contains no data
  34. }
  35. $start_tag = "<".$elm;
  36. foreach($propBag as $key => $value){
  37. $attributes .= " $key=\"$value\"";
  38. }
  39. if(!$self_closing){
  40. $start_tag .= " ".$attributes.">";//CLOSE THE START_TAG AFTER SETTING ALL THE ATTRIBUTES FOR THE ELEMENT
  41. $end_tag = "</".$elm.">"; //BUILD THE CLOSING TAG
  42. $html = $start_tag.$contained_data.$end_tag; //WRAP THE CONTAINED DATA IN THE START AND END TAGS
  43.  
  44. }else{
  45. $start_tag .= " ".$attributes;
  46. $end_tag = '/>';
  47. $html = $start_tag.$end_tag;
  48. }
  49. return $html;
  50. }
  51.  
  52. //EXAMPLES
  53. //FORM LEGEND
  54. echo html("legend",array(
  55. "_data"=>"Form Legend"
  56. ));
  57. //FORM TEXT INPUT AND LABEL
  58. //may also be used for checkbox,radio,and textarea
  59. echo html("label",array(
  60. "for"=>"function_test",
  61. "_data"=>"html function test"
  62.  
  63. ));
  64. echo html("input",array(
  65. "name"=>"function_test",
  66. "type"=>"text",
  67. "class"=>"input-xxlarge",
  68. "placeholder"=>"i guess it worked", //CUSTOM ATTRIBUTE
  69. "rel"=>"tooltip",
  70. "title"=>"this is my tooltip",
  71. "style"=>"border:10px solid red;",
  72. "onchange"=>"alert('boo hoo');" //EXECUTE JS TRIGGERED BY EVENT
  73. ));
  74. //IMAGE TAG
  75. echo html("img",array("alt"=>"","src"=>"images/logo.png","height"=>"50"));
  76. //HYPER_LINK
  77. echo html("a",array(
  78. "id"=>"myanchor",
  79. "href"=>"http://www.google.com",
  80. "_data"=>"link text"
  81. ));
  82. //HYPERLINK WITH AN IMAGE AS CONTAINED DATA
  83. $images .= html("a",array(
  84. "class"=>"lightbox",
  85. "href"=>$filename,
  86. "_data"=> html("img",array(
  87. "src"=>$filename,
  88. "class"=>"thumbnail img-polaroid span2",
  89. "alt"=>""
  90. ))
  91. ));
  92. //META TAG
  93. echo html("meta",array(
  94. 'name' => 'Content-type',
  95. 'content' => 'text/html; charset=utf-8',
  96. 'type' => 'equiv'
  97. ));

以上是关于HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素的主要内容,如果未能解决你的问题,请参考以下文章

重新认识JavaScript里的创建对象

变速动画函数封装增加任意多个属性值

13.面向对象与继承

JS---封装缓动(变速)动画函数---增加任意多个属性&增加回调函数

JavaScript创建对象的常用模式

变速动画函数封装增加任意多个属性透明度和层级