Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在呈现PHP对象时向HTML添加Javascript引用?_Javascript_Php_Jquery_Html - Fatal编程技术网

如何在呈现PHP对象时向HTML添加Javascript引用?

如何在呈现PHP对象时向HTML添加Javascript引用?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我们在这里使用许多PHP模块来创建用于构建web页面的对象 我们有以下模块: 锚(ahref) 钮扣 复选框 组合框 日期时间 电子邮件 标签 注 密码 电话 单选按钮 RichTextArea 提交按钮 文本(文本框) 通过调用项目的render()方法,将每个对象转换为PHP的HTML;但是,没有一个模块包含javascript 我想创建一个包含标准联系人元素(姓名、地址、城市、州、电话、电子邮件等等)的联系人模块 我已经用jquery语法创建了一个javascript文件来验证我的联系

我们在这里使用许多PHP模块来创建用于构建web页面的对象

我们有以下模块:

  • 锚(ahref)
  • 钮扣
  • 复选框
  • 组合框
  • 日期时间
  • 电子邮件
  • 标签
  • 密码
  • 电话
  • 单选按钮
  • RichTextArea
  • 提交按钮
  • 文本(文本框)
通过调用项目的
render()
方法,将每个对象转换为PHP的HTML;但是,没有一个模块包含javascript

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

我已经用jquery语法创建了一个javascript文件来验证我的联系人块中的控件,但是我无法将我的标记放在HTML的部分

还有办法让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);
}
公共函数render(){
$output='\n';
//其他代码…$output.=''.$this->groupName'.\n';
回报(产出);
}
更新:

剪断以避免侵犯版权

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

我想用javascript添加我的联系人块元素来验证字段

$form=new HTMLForm('Blank Page')
创建带有标记的HTML页面

如果标记已关闭,是否有方法通过验证javascript添加我的联系人块

更新2:

剪断以避免侵犯版权


在不完全了解您的框架的情况下,我不得不说您可能需要一个
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);
        }
    }

?>
下面是如何做到这一点

首先,创建这个类:

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

?>

return
更改为
echo
…除非他调用此函数并回显返回值,否则我认为我们需要更多的代码。您正在加载jQuery吗?是否在加载jQuery之前将其呈现到页面?你确定调用JS函数是为了启动它吗?等等。我将添加一些详细信息。您能否为我们演示一个示例,例如如何创建
按钮
?谢谢。我现在要删除这些类,因为我担心它们可能是专有的。如果这是一个众所周知的模型类型,请让我知道,我会将其链接到中,以便其他人现在了解您是如何获得此最终结果的!:)我没有它的名字,但我几乎重写了
PhpLabel
类,这样它就可以创建一个格式正确的
标记。另外,如果您担心代码泄漏,那么我应该告诉您StackOverflow跟踪得非常好的编辑历史记录。在你的帖子下面,你应该会看到一个链接,上面写着
在X分钟/天/月前编辑过
,但总体来说,你展示的东西不足以让我学到一些我还不知道的东西=)有趣的方法,而且它符合我们目前使用的方法。我当然也会尝试这种方法。注意:我将删除我粘贴在更新部分的类,因为它们可能是专有的。如果这是一个众所周知的PHP模型,请告诉我,我会将其链接到中或按名称引用,以便其他人现在了解您是如何得到这个最终结果的!:)@jp2code您能将此标记为答案吗?我还将检查该代码的适当性,因为如果它是开源的或允许发布在这里(这是非常简单的代码),理解这一点会很有帮助。
<?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" );