有更好的方法来更改php中的DOMElement-> tagName属性吗? [英] Is there a better way to change a DOMElement->tagName property in php?

查看:69
本文介绍了有更好的方法来更改php中的DOMElement-> tagName属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚碰到这个构建一个我的MVC框架的Textbox控件,其中刚刚完成整个文档之前,我在继承自ServerTag(又从DOMElement继承)的所有东西称之为PreRender。

I just ran into this building a Textbox control for my MVC framework, where just before finalizing the whole document I call PreRender on everything that inherits from ServerTag (which in turn inherits from DOMElement).

我发现更改DOMElement派生对象的tagName的唯一方法是将其替换为新的,所有属性都与旧的属性同步。

The only way i have found to change a DOMElement derived object's tagName is to replace it with a new one with all the attributes synced to the old one.

这是代码中的代码:

protected function PreRenderTextarea( WebPage $sender )
{
    $textarea = $sender->createElement( 'textarea' );
    foreach( $this->attributes as $attribute )
    {
        if ( $attribute->name == 'value' )
        {
            $textarea->nodeValue = $attribute->value;
        }
        else
        {
            $textarea->setAttribute( $attribute->name, $attribute->value );
        }
    }
    $this->parentNode->replaceChild( $textarea, $this );
}

public function OnPreRender( WebPage $sender )
{
    parent::OnPreRender();
    $this->setAttribute( 'value', $this->Value );

    switch( $this->Mode )
    {
        case 'normal' : 
            $this->setAttribute( 'type', 'text' ); 
            break;
        case 'password' : 
            $this->setAttribute( 'type', 'password' ); 
            break;
        case 'multiline' : 
            $this->PreRenderTextarea( $sender ); 
            return; 
            break;
    }
}

这是真的唯一的办法吗?这样做有一个相当不利的副作用,使所有控制背后的逻辑失效。

Is that really the only way to do it? This way has the rather unwanted side effect of nulling all the logic behind the control.

推荐答案

是的,你该怎么做它的原因是您不只是更改单个属性( tagName )的值,而是将整个元素从一个类型更改为另一个。属性如 tagName (或 nodeName )和 nodeType 在DOM中只读,并在创建元素时设置。

Yes, this how you have to do it -- the reason is that you're not just changing the value of a single attribute (tagName), you're actually changing the entire element from one type to another. Properties such as tagName (or nodeName) and nodeType are read-only in the DOM and set when you create the element.

所以,创建一个新的元素,代替旧的元素,正如你所做的那样,与 DOMNode :: replaceChild ,是正确的操作。

So, creating a new element and moving in place of the old one exactly as you're doing, with DOMNode::replaceChild, is the correct operation.

我不知道你的意思是什么否定所有控制背后的逻辑的副作用 - 如果你澄清我可能可以给你指导。

I'm not sure what you mean by "unwanted side effect of nulling all the logic behind the control" -- if you clarify I might be able to give you guidance there.

听起来你可能不想要ServerTag继承自DOMElement,而您可能希望通过某些其他模式(例如组合)链接这两个对象(即,ServerTag具有DOMElement而不是是DOMElement),因此您只是替换DOMElement对象与您的ServerTag Textbox对象相关联。

It sounds like you might not want to have ServerTag inherit from DOMElement and instead you may want to link these two objects through some other pattern, such as composition (i.e. so a ServerTag "has a" DOMElement instead of "is a" DOMElement) so that you're merely replacing the DOMElement object associated with your ServerTag Textbox object.

或者长时间猜测,您可能会遇到问题,只需复制属性(即 textarea 具有必需的属性,如 cols 输入不)。

Or a longer-shot guess is you might be running into issues just copying the attributes (i.e. textarea has required attributes, like rows and cols, that input does not).

这篇关于有更好的方法来更改php中的DOMElement-> tagName属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆