前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ext applyTo和renderTo的区别和用法「建议收藏」

Ext applyTo和renderTo的区别和用法「建议收藏」

作者头像
全栈程序员站长
发布2022-09-19 18:26:34
3420
发布2022-09-19 18:26:34
举报

大家好,又见面了,我是你们的朋友全栈君。 extjs中经常会用到renderTo或applyTo配置选项。这里,我就比较下两者的区别与用法。 1、renderTo与render方法对应 2、applyTo与applyToMarkup方法对应

一、applyTo的使用: 1、applyTo所指向的el元素必须要有父节点。 2、applyTo所指向的el元素实际上是充当了对象要渲染的模板,对象是渲染在其父节点内。即对象实例化后所产生的html代码是插入在el元素的父节点内,而el元素本身将只作为模板,并不作为真正的在其位置上的元素,既然作为模板,只是利用其标签内的部分style和class,就不应该包含子节点(包括文本)。 3、这个作为模板的el元素很重要,必须是要存在的。

4、示例代码:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<title>applyTo与renderTo的区别</title>  
<link rel="stylesheet" type="text/css" href="../scripts/ext/resources/css/ext-all.css"/>  
<script type="text/javascript" src="../scripts/ext/adapter/ext/ext-base.js"></script>  
<script type="text/javascript" src="../scripts/ext/ext-all.js"></script>  
<script type="text/javascript">  
    Ext.onReady(function(){  
        var _panel = new Ext.Panel({  
            title:"个人信息",  
            width:300,  
            height:300,  
            frame:true,  
            applyTo:"appConId"  
        });  
    });  
</script>  
</head>  
<body>  
    <div id="appId" style="padding:30px;width:500px;height:400px;background-color: blue;">  
        <div id="appConId" style="width:400px;height:400px;background-color:green;"></div>  
    </div>  
</body>  
</html> 

5、效果图:

Ext applyTo和renderTo的区别和用法「建议收藏」
Ext applyTo和renderTo的区别和用法「建议收藏」

此时,appConId元素作为了模板,其width样式并没有被应用上,而其他的height和background-color样式被应用上了。就相当于这个div被替换或改造了。

二、renderTo的使用: 1、可以有el配置选项。 2、如果有el配置选项,则其指向的el元素充当了模板,并且必须存在。 3、renderTo所指向的el元素将作为对象渲染的入口,即render所产生的html代码将作为renderTo所指向的el元素的子节点。 4、如果有el配置选项,那么render会将el配置选项所指向的el元素作为模板然后产生html代码作为renderTo所指向的el元素的子节点。 5、示例代码:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<title>applyTo与renderTo的区别</title>  
<link rel="stylesheet" type="text/css" href="../scripts/ext/resources/css/ext-all.css"/>  
<script type="text/javascript" src="../scripts/ext/adapter/ext/ext-base.js"></script>  
<script type="text/javascript" src="../scripts/ext/ext-all.js"></script>  
<script type="text/javascript">  
    Ext.onReady(function(){  
        var _panel = new Ext.Panel({  
            title:"个人信息",  
            width:300,  
            height:300,  
            frame:true,  
            el:"elId",  
            renderTo:"appConId"  
        });  
    });  
</script>  
</head>  
<body>  
    <div id="appId" style="padding:30px;width:500px;height:400px;background-color: blue;">  
        <div id="appConId" style="width:400px;height:400px;background-color:green;"></div>  
    </div>  
    <div id="elId" style="width:500px;height:400px;background-color:red;">  
    </div>  
</body>  
</html> 

6、效果图:

Ext applyTo和renderTo的区别和用法「建议收藏」
Ext applyTo和renderTo的区别和用法「建议收藏」

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/164716.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档