如何在渲染PHP对象时添加Javascript对HTML的引用?

时间:2014-01-07 21:03:29

标签: javascript php jquery html

我们在这里使用了很多PHP模块来创建用于构建我们网页的对象。

我们有以下模块:

  • 主播(ahref)
  • 按钮
  • 复选框
  • 组合框
  • 日期时间
  • 电子邮件
  • 标签
  • 注意
  • 密码
  • 电话
  • 单选按钮
  • RichTextArea
  • 提交按钮
  • 文字(文字框)

通过调用项目的render()方法,将每个对象转换为PHP的HTML;但是,没有一个模块包含javascript。

我想创建一个联系人阻止模块,其中包含标准联系人元素(姓名,地址,城市,州,电话,电子邮件,等等等)。

我使用jquery语法创建了一个javascript文件来验证联系人阻止中的控件,但是我无法将 标记放在HTML的 < / strong> section。

还有办法让javascript工作吗?

public function render() {
  $output = '<script type="text/javascript" src="/lib/js/ContactBlock.js"></script>\n';
  // other code ... $output .= '<fieldset><legend>'.$this->groupName.'</legend>\n';
  return($output);
}

更新

  

为了避免侵犯版权而窃听

这可能过于简化,但基本上它会将HTML呈现给浏览器。

我想用javascript添加联系人阻止元素来验证字段。

$form = new HTMLForm('Blank Page')行创建了HTML页面,其中已包含 标记。

如果 标记已经关闭,我是否可以通过验证javascript添加联系人阻止

更新2:

  

为了避免侵犯版权而窃听

2 个答案:

答案 0 :(得分:1)

在不完全了解您的框架的情况下,我不得不说您可能需要PhpJsScript类:

<?php

    class PhpJsScript extends BasicHTMLEntity {

        public function __construct($url) {

            // Set element type
            $this->setElementType('script');

            // Specify behavior of element value
            $this->entity_value_as_content = true;

            // Specify default attributes
            $this->attr('type', 'text/javascript');
            $this->attr('src', $url);
        }

        public function render(){
            // Open element
            $output = "<{$this->getElementType()}{$this->renderAttributes()}";

            // Close element
            $output .= "</{$this->getElementType()}>";

            // Return rendered object
            return($output);
        }
    }

?>

所以现在我觉得你可以用它来打电话:

$script = new PhpJsScript('/lib/js/ContactBlock.js');

答案 1 :(得分:1)

这是怎么做的。

首先,创建此类:

<?php

//Named this way so you can make any element tag
class PhpFreeElement extends BasicHTMLEntity {

    private $strong = false;

    public function __construct($tag_type, $element_name) {

        // Set element type
        $this->setElementType( $tag_type );

        // Specify behavior of element value
        $this->entity_value_as_content = true;

        // Specify default attributes
        if ( !empty( $name ) ) {
            $this->attr('name', $element_name);
            $this->attr('id', $element_name);
        }
    }

    public function setAttribute($name, $value)
    {
        $this->attr( $name, $value );
    }


    public function render(){

        // Open element
        $output = "<{$this->getElementType()}{$this->renderAttributes()}";

        //Add the value if any
        $output .= ">{$this->getValue()}";

        // Close element (This is not always correct. Some tags are self closing)
        $output .= "</{$this->getElementType()}>";

        // Return rendered object
        return($output);
    }
}

?>

然后你就像这样创建它:

//Create script tag
$script = new PhpFreeElement('script', '');

//Set the script source
$script->setAttribute( "src", "/lib/js/ContactBlock.js" );