从下面的代码中,我如何将可重用性应用于javascript函数?

从下面的代码中,我如何将可重用性应用于javascript函数?,javascript,jquery,html,Javascript,Jquery,Html,javascript window.onload=function() { document.getElementById("next").onclick=function() { document.getElementById("out_est_value").innerHTML=document.getElementById("est_value").value; document.getElementById("out_int_est_value").innerHTML

javascript

window.onload=function() {
  document.getElementById("next").onclick=function() {
    document.getElementById("out_est_value").innerHTML=document.getElementById("est_value").value;
    document.getElementById("out_int_est_value").innerHTML=document.getElementById("est_value").value*0.75;
  }
}
输入

物业估价:
输出

<p> You told us the estimated value of your property is &pound;<span class="red" id="out_est_value"></span> based on this we estimate that the initial cash offer is likely be around &pound;<span class="red" id="out_int_est_value"></span>.<p>
您告诉我们您的房产估计价值为英镑;基于此,我们估计初始现金报价可能在英镑左右

只要有可能,将在DOM中找到的对象(例如
getElementById(x)
)或通过在单独的变量中查找属性(例如
element.value
)来访问的对象存储在一个单独的变量中,如果这些对象将被多次访问(基本上是“重用”它们,而不是再次查找它们)。这样做将提高代码的可读性和性能:

window.onload=function(){
var estVal=document.getElementById(“est_值”)
,outEstVal=document.getElementById(“out\u est\u值”)
,outinestval=document.getElementById(“out_int_est_value”);
document.getElementById(“next”).onclick=function(){
var val=estVal.value;
outEstVal.innerHTML=val;
outinestval.innerHTML=val*0.75;
};
};
此外,如果您真的在使用jQuery,那么请接受它为您所做的方便的事情(这非常值得!),例如查找DOM元素、添加事件处理程序、设置HTML内容等。这样做可能会提高您自己和其他开发人员代码的可维护性,并帮助您避免常见的跨浏览器陷阱:

$(文档).ready(函数(){
var estVal=$(“#est_值”)
,outEstVal=$(“#out_est_value”)
,outinestval=$(“#out_int_est_value”);
$(“#下一步”)。单击(函数(){
var val=estVal.val();
html(val);
outinestval.html(val*0.75);
});
});

问题到底是什么?您希望在将来重用代码的哪一部分?我想重用“est_value”的值,并将其显示在另一个屏幕上。您可以对jQuery部分使用
.html()
而不是
.innerHTML
。@本·阿尔伯特:是的,刚刚修复了jQueryism…=)@本·阿尔伯特:上面的代码不起作用,但是,你的这段代码非常出色,谢谢你,先生:)@tony:下面是一个使用我上面代码的例子。
<p> You told us the estimated value of your property is &pound;<span class="red" id="out_est_value"></span> based on this we estimate that the initial cash offer is likely be around &pound;<span class="red" id="out_int_est_value"></span>.<p>