在字符串中执行javascript函数

nee*_*meg 1 html javascript

除了下面指定的代码之外,有没有更好的方法将动态文本注入html字符串?

var code ='siteCode1';

html代码作为字符串:

existing: '<p>Log in with your'+ eval("siteVarObj('+code+').siteName") +'account</p>',


function siteVarObj(siteCode){
            if(siteCode === 'siteCode1'){
                this.siteName = 'google.com';
                this.siteContactUsURL = '';
            }else if(siteCode === 'siteCode2'){
                this.siteName = 'stackoverflow.com';
                this.siteContactUsURL = '';
            }
            return this;
        }
Run Code Online (Sandbox Code Playgroud)

BLS*_*lly 5

这应该工作得很好:

var code = 'siteCode1';
var myHTMLString = '<p>Log in with your '+ siteVarObj(code).siteName + ' account</p>';
Run Code Online (Sandbox Code Playgroud)