我有一个问题,我一直想弄清楚,但问题是,我不知道我被要求做什么,所以我甚至不知道从哪个问题开始。我在编码方面没有受过教育,根本不知道我的第一步是什么以及我将如何处理这个问题。这个问题给了我HTML代码并问我以下内容:
编写一个以两个字符串作为参数的函数replace(tag,value)。第一个是标签(例如,“item”(没有引号),第二个是替换值(例如,“flux flux”(没有引号)。此函数应该使用指定的给定标签替换元素的innerHTML值。
如果标签不存在,您的功能应显示警告,指示未找到标签。通过定义format()函数来测试代码,该函数与格式按钮相关联,将'salutation'替换为'Mr.史密斯','发票'用123,'item'用'flux capacitor'和'威胁'用'请不要让我生气'。
这个问题要我做什么?对我来说这听起来像是胡言乱语(虽然我确定不是)但我在网上搜索过但我还没有看到任何有用的东西。
以下是HTML代码:
<html>
<head>
<script src="q1.js" type="text/javascript"> </script>
</head>
<body>
Dear <span id="salutation">Name</span>;
<p>
It has come to our attention that your invoice <span id="invoice">ID</span>
has yet to be paid. It has now been <span id="time">some time</span> since
you received <span id="item">the material</span> from Evil Incorporated. Please
remit payment immediately. <span id="threaten"></span>
</p>
Yours sincerely,<br>
<br>
<br>
J. Smith, Accounting
<div id="buttons">
<center>
<button onclick="format()">Format</button>
<button onclick="clearit()">Clear</button>
</center>
</div>
</body>
</html>
答案 0 :(得分:0)
也许这就是你要找的......:D
function format(){
Replace('salutation','Mr. Smith');
Replace('invoice','123');
Replace('item','flux capacitor');
Replace('threaten','Please dont make me angry');
}
function clearit(){
Replace('salutation','Name');
Replace('invoice','ID');
Replace('item','the material');
Replace('threaten','');
}
function Replace(tag,value){
if($('body').find('#'+tag)){
$('#'+tag).html(value);
}
else{alert('Tag not found');}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script src="q1.js" type="text/javascript"> </script>
</head>
<body>
Dear <span id="salutation">Name</span>;
<p>
It has come to our attention that your invoice <span id="invoice">ID</span>
has yet to be paid. It has now been <span id="time">some time</span> since
you received <span id="item">the material</span> from Evil Incorporated. Please
remit payment immediately. <span id="threaten"></span>
</p>
Yours sincerely,<br>
<br>
<br>
J. Smith, Accounting
<div id="buttons">
<center>
<button onclick="format();">Format</button>
<button onclick="clearit();">Clear</button>
</center>
</div>
</body>
</html>