如何在 DOM 中将参数作为百分比传递给 javascript 中的 element.setAttribute?

How do i pass a parameter as a percentage in DOM in a element.setAttribute in javascript?

better if viewed in img uploaded

function create(R,A){
  var B=R; 
  var C=A;          

  // if i define a number e.g. 50% it works but when i 
  //pass a value it doesn't
  img.style.top = "B%";             
  img.style.left = "C%";// same with c
}

试试这个

function create(R,A){    
var B=R; 
var C=A;          

img.style.top = B + "%";            
img.style.left = C + "%";
}

您的代码无效,因为您已通过 "B%" 样式 属性,这是不正确的。你应该试试 B+ "%".

你可以这样做

function create(R,A){    
img.style.top = R+"%";
//pass a value it doesn't.            
img.style.left = C+"%";
}

此外,除非您要修改函数的参数,否则无需创建变量