[英]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: 我们有以下模块:
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.