SVG动态修改Text的文本
2016-12-20 09:49
906 查看
SVG Text的文本由于不是他的属性,因此不能采用setAttribute来更改。
下面说两种不同方式实现:
1、通过改变他的textContent
例如:svg内容如下,但是非常遗憾这种方法不适合IE6。Opera10是可以的
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
onload="init()">
<script>
<![CDATA[
var text, textnode
function init()
{
textnode = document.getElementById("text1")
}
function changeText()
{
text = textnode.textContent
textnode.textContent = "test"
}
function restoreText()
{
textnode.textContent = text
}
]]>
</script>
<text id="text1" x="300" y="200" font-size="60" font-family="tempus sans itc" stroke="red" stroke-width="2" onmouseover="changeText()" onmouseout="restoreText()">
OMG
</text>
</svg>
2、第二种方法创建一个新的TextSpan替换旧的经过在IE6下测试好用。其他的浏览器没有试过
例如:下面是主要函数
function changeDescriptionText(evt,siteNum)
{
var newDescriptionText = svgDocument.createTextNode("Click here to goto DeveloperWorks.");
targetText.replaceChild(newDescriptionText,targetText.getFirstChild());
}
下面说两种不同方式实现:
1、通过改变他的textContent
例如:svg内容如下,但是非常遗憾这种方法不适合IE6。Opera10是可以的
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
onload="init()">
<script>
<![CDATA[
var text, textnode
function init()
{
textnode = document.getElementById("text1")
}
function changeText()
{
text = textnode.textContent
textnode.textContent = "test"
}
function restoreText()
{
textnode.textContent = text
}
]]>
</script>
<text id="text1" x="300" y="200" font-size="60" font-family="tempus sans itc" stroke="red" stroke-width="2" onmouseover="changeText()" onmouseout="restoreText()">
OMG
</text>
</svg>
2、第二种方法创建一个新的TextSpan替换旧的经过在IE6下测试好用。其他的浏览器没有试过
例如:下面是主要函数
function changeDescriptionText(evt,siteNum)
{
var newDescriptionText = svgDocument.createTextNode("Click here to goto DeveloperWorks.");
targetText.replaceChild(newDescriptionText,targetText.getFirstChild());
}
相关文章推荐
- svg 动态修改text文本
- SVG动态修改Text的文本
- SAP 登陆界面菜单文本修改 (Modification of SAP Logon Menu text)
- svg中动态更改text属性值
- iOS 根据文本内容为TextView动态定义高度
- IOS动态获取文本的高度--自适应文本宽度、高度Textview
- c# 动态修改水晶报表文本内容
- 动态修改EditText输入键盘并隐藏或显示密码
- jquery动态增加text 元素以及删除文本内容
- jquery动态增加text元素以及删除文本内容实例代码
- 修改文本CREATE_TEXT/SAVE_TEXT/RE…
- wince和Windowsmobile开发中怎样动态修改menubar菜单项的文本(原)
- 动态创建Text或者Label并且控制文本颜色以及透明度
- Ext无法动态修改 tbar 中label或TextItem 中Text的解决办法
- 动态修改报表选择界面选择文本
- android 动态修改textview背景
- svg:text属性的可见性的修改(待解决问题)
- 使用TextWatcher监听EditText的文本变化之后动态改变EditText里面的内容
- android 动态修改 TextView的width属性
- andorid,动态设置TextView文本中字体的颜色和格式,用Html.fromHtml方法