简体   繁体   English

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

[英]How To Add Javascript Reference to HTML when Rendering a PHP object?

We use a lot of PHP modules here to create objects that are used to build our web pages. 我们在这里使用了很多PHP模块来创建用于构建我们的网页的对象。

We have modules for: 我们有以下模块:

  • Anchor (ahref) 锚(ahref)
  • Button 按键
  • CheckBox 复选框
  • ComboBox 组合框
  • DateTime 约会时间
  • Email 电子邮件
  • Label 标签
  • Note 注意
  • Password 密码
  • Phone 电话
  • RadioButton 单选按钮
  • RichTextArea RichTextArea
  • SubmitButton 提交按钮
  • Text (TextBox) 文字(TextBox)

Each object is converted to HTML for the PHP by calling the item's render() method; 通过调用item的render()方法render()每个对象转换为PHP的HTML; however, none of the modules contain javascript. 但是,没有一个模块包含javascript。

I want to create a Contact Block module that will contain standard contact elements (name, address, city, state, phone, email, blah-blah-blah). 我想创建一个Contact Block模块,其中包含标准的联系人元素(姓名,地址,城市,州,电话,电子邮件,等等等等)。

I have created a javascript file with my jquery syntax to validate the controls in my Contact Block , but I cannot get my 我用我的jquery语法创建了一个javascript文件来验证我的联系人阻止中的控件,但我无法得到我的 tag placed in the HTML's 标签放在HTML中 section. 部分。

Is there still a way to get the javascript to work? 还有办法让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);
}

UPDATE: 更新:

snipped to avoid copyright infringement 剪掉以避免侵犯版权

This may be too simplified, but basically it renders the HTML to the browser. 这可能过于简化,但基本上它将HTML呈现给浏览器。

I want to add my Contact Block elements with javascript to validate fields. 我想用javascript添加我的Contact Block元素来验证字段。

The line $form = new HTMLForm('Blank Page') creates the HTML page with the $form = new HTMLForm('Blank Page')创建带有的HTML页面 tag already in it. 标签已经在其中。

Is there a way for me to add my Contact Block with validating javascript if the 有没有办法让我添加我的联系人块与验证JavaScript如果 tag has already been closed? 标签已经关闭?

UPDATE 2: 更新2:

snipped to avoid copyright infringement 剪掉以避免侵犯版权

Without knowing your framework to the full extent I would have to say that you probably need a PhpJsScript class: 在不完全了解您的框架的情况下,我不得不说您可能需要一个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);
        }
    }

?>

So now I think you can call it with: 所以现在我认为你可以用:

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

Here's how to do it. 这是怎么做的。

First, create this class: 首先,创建这个类:

<?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);
    }
}

?>

Then you create it like this: 然后你像这样创建它:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用html画布,使用JavaScript和php时图像渲染模糊 - image rendering blurry when using html to canvas, using JavaScript and php 当html id是php变量时,如何在选中复选框时添加并在Javascript中未选中时扣除 - How to add when a checkbox is checked and deduct when unchecked in Javascript when html id is a php variable 如何将JavaScript对象数据添加到HTML - How to add JavaScript object data to HTML 如何使用html / javascript添加对象的链接? - how add link for object with html/javascript? 如何将这些 HTML 属性添加到 JavaScript object? - How to add these HTML attributes to a JavaScript object? 如何在PHP退出后添加html和javascript - how to add html & javascript after PHP exit 如何以点表示法引用 HTML object 'name' 属性 - JavaScript - How to reference an HTML object 'name' attribute in dot notation - JavaScript 您如何在javascript中引用此html对象? 苹果脚本 - how do you reference this html object in javascript? applescript 如何在JavaScript中触发事件的情况下检索对html对象的引用? - How to retrieve the reference to an html object the fired an event in JavaScript? 如何在javascript多级对象中动态添加对父对象的引用? - How to dynamically add reference to parent in a javascript multilevel object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM