如何替换元素属性中的部分文本

How to replace part of text in element attribute

本文关键字:文本 属性 元素 何替换 替换      更新时间:2023-09-26

您好,我想将 textbox1 值放在下面的代码中"在此处输入代码"的位置。 请帮助我。

<script >
 function myfunction() { 
            var first = document.getElementById("textbox1").value;
            var textbox3 = document.getElementById('textbox3');
            textbox3.value=first;
          } 
    </script>
 <body> 


 <input type="text" name="textbox3" id="textbox3" readonly="true"/>
 <object type="application/x-shockwave-flash" data="Working.swf" width="600"  
         height="600">
   <param name= background-color="Black"/>
   <param name="movie" value="Working.swf" />
   <param name="flashvars" value="ImgUrl=`enter code here`" />
</object>

** 应该将问题更改为 - "如何替换元素属性中的部分文本"

使用javascript很容易做到。

你可以在这里玩 http://jsfiddle.net/6np2g6kw/,可能会学到一些东西。

或获取下面的代码

        var first = document.getElementById("textbox1").value;
        var textbox3 = document.getElementById('textbox3');
        textbox3.value=first;
        //getting the flash object element
        var flash_object = document.getElementById("myObject");
        //getting the desired param by its name
        var flash_vars = flash_object.getElementsByTagName('param')[2];
        //getting the value attribute value
        var falsh_vars_value = flash_vars.attributes.value.textContent;
        //splitig by comma to seperate the "enter code here"
        falsh_vars_value = falsh_vars_value.split('`');
        // this option is more generic
        //building the new string the the 'textbox1' value between the commas
        var flash_vars_new_value = falsh_vars_value[0] +'`' + first + '`' + falsh_vars_value[2];
        // this is and options as well
        //falsh_vars_value = falsh_vars_value.replace('enter code here', first);
        //you can alow do with regex its abit more complex.
        // read here - http://regexone.com/
        //setting the new value to the element
        flash_vars.attributes.value.textContent= flash_vars_new_value;