javascript - jQuery ("#"+id) 对比 document.getElementById (""+id)

标签 javascript jquery html innerhtml

我的一个 div id="ftbutton_1357567084/Team:8/User:10/Image:195" 有以下 ID,我想在调用 ajax 后替换它的 html。

当我尝试使用 jQuery 执行此操作时,如下所示的操作不起作用

jQuery("#"+id).html(SOME HTML HERE);

下面的代码在哪里工作

document.getElementById(id).innerHTML = SOME HTML HERE;

我知道ID不应该包含一些特殊字符

ID attributes should be = after an initial letter (a-z or A-Z), may also use 
periods and colons, in addition to letters, numbers, hyphens, and underscores. 

引用 How do I select an element by an ID that has characters used in CSS notation?

但由于某些原因,我无法更改每个元素的 ID,因为它被认为是网站。

我也试过follow,但是不行

function jq( myid ) {
  return "#" + myid.replace( /(:|\.)/g, "\\$1" );
}
jQuery(jq("#"+id)).html(SOME HTML HERE);

我只是想知道是否是这种情况,我是否需要使用 document.getElementById(""+id) 而不是 jQuery("#"+id) ?

或者换句话说

document.getElementById(""+id) 是否比 jQuery("#"+id) 更可靠?

最佳答案

这样做:

$('[id="ftbutton_1357567084/Team:8/User:10/Image:195"]');

这实际上是一个有点有趣的问题。 jQuery 很可能认为 : 正在启动一个过滤器,例如 $('div:visible') 并在没有意识到它是一个完整 ID 的情况下陷入困境。

关于javascript - jQuery ("#"+id) 对比 document.getElementById (""+id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254651/

相关文章:

javascript - 根据 Ajax 调用中收到的数据 react 设置状态

javascript - 加载 JS 库并在加载后立即使用它们

javascript - 在 javascript(typescript) 中格式化数字

javascript - 绑定(bind)后获取kendo scheduler数据源

javascript - 图表未正确渲染,canvasjs

javascript - 将单击按钮和在表单上按下 "enter"键的 if 语句条件组合起来

jQuery:流体同位素仅在调整大小后起作用

javascript - 使用 javascript 数组显示特定的 HTML div

javascript - AngularJS 从不良数据中过滤掉字符串。

javascript - Canvas - IndexSizeError : Index or size is negative or greater than the allowed amount