简体   繁体   English

getElementById()的Javascript速记语法不起作用

[英]Javascript shorthand syntax for getElementById() not working

I've found an answer to a previous question about javascript short hand for this https://stackoverflow.com/a/6398848/1406888 but it is not working for me. 我已经找到了关于此https://stackoverflow.com/a/6398848/1406888的 javascript快捷方式的先前问题的答案,但它对我不起作用。 I don't understand it because when I use the same short hand in the firefox developer's console it works. 我不明白,因为当我在firefox开发人员控制台中使用相同的快捷键时,它可以工作。

Here's a short example of the code I'm using 这是我正在使用的代码的简短示例

<div id="list1">
<li>pres</li>
<li>mike</li>
<li>camel</li>
<li>week</li>
</div>

<p></p>
<div id="list2">
<li>time</li>
<li class="test">era</li>
<li>people</li>
<li>life</li>
</div>

<p></p>
<div id="list3">
<li>time</li>
<li class="test">era</li>
<li>people</li>
<li>life</li>
</div>

<script type="text/javascript">

var testId = document.getElementById("list1");
testId.style.borderStyle="solid";
$("#list2").style.borderStyle="solid";

$("list3").style.borderStyle="solid";

</script>

The only one that works is the list1 border. 唯一起作用的是list1边框。 The other ones fail. 其他失败。 Can somebody please correct me. 有人可以纠正我吗?

I now have a working template. 我现在有一个工作模板。 Here is my code: 这是我的代码:

<div id="list1">
<li>pres</li>
<li>mike</li>
<li>camel</li>
<li>week</li>
</div>

<p></p>
<div id="list2">
<li>time</li>
<li class="test">era</li>
<li>people</li>
<li>life</li>
</div>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

var testId = document.getElementById("list1");
testId.style.borderStyle="solid";
//$("#list2").style.borderStyle="solid";   this does not work
$("#list2").css("border-Style","solid");   this works.
</script>

Just so I get this straight. 只是为了让我明白这一点。 There is not substitute for getElementById??? 不能替代getElementById ??? I thought it was an interchangable short-hand, but in my example it only works with the .css property of the JQuery shorthand. 我以为它是可互换的简写,但是在我的示例中,它仅与JQuery简写的.css属性一起使用。

Sometimes when I just want to be able to use the $ as a shorthand for document.getElementById without actually requiring the functions of major javascript libraries, I'll just toss this in the page: 有时,当我只希望能够将$用作document.getElementById的简写而又不实际需要主要javascript库的功能时,我会在页面中扔掉它:

function $(a){return document.getElementById(a);}

Saves a ton of typing. 节省了大量的打字时间。

You need to have some library to provide this shortcut (like jQuery). 您需要一些库来提供此快捷方式(例如jQuery)。 It is not built into the core language or your browser. 它没有内置在核心语言或浏览器中。

Also, for jQuery, it would be $('#list3') (you can use all kinds of selectors, not just the node id). 另外,对于jQuery,它是$('#list3') (您可以使用各种选择器,而不仅仅是节点ID)。

您需要在页面上包括JQuery才能使用此表示法。

The syntax that you have described here requires the jQuery javascript library. 您在此处描述的语法需要jQuery javascript库。 You have to download and import the library, or link directly to the jQuery code bank in your HTML before this will work. 您必须下载并导入该库,或者直接链接到HTML中的jQuery代码库,然后才能运行。 Add the following line to the top of the head in your HTML file: 将以下行添加到HTML文件的头部顶部:

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

append jquery library, $ is not in the core functionality -- 追加jquery库,$不在核心功能中-

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

var testId = document.getElementById("list1");
testId.style.borderStyle="solid";
$("#list2").style.borderStyle="solid";

$("list3").style.borderStyle="solid";

</script>

OR if your hard to JS then do it in this way---- 或者,如果您不喜欢JS,则可以通过这种方式进行操作----

var testId1 = document.getElementById("list1");
var testId2 = document.getElementById("list2");
var testId3 = document.getElementById("list3");
testId1.style.borderStyle="solid";
testId2.style.borderStyle="solid";
testId3.style.borderStyle="solid";

IMO: you need to do it like this in jquery IMO:您需要在jquery中这样做

$("#list2").css("border-Style","solid")

$("#list3").css("border-Style","solid")

and make sure in jquery you add # with Id if you are accessing an element with id. 并确保在jquery中,如果要访问具有ID的元素,请在ID中添加#

Try setting the border style using the jquery css method. 尝试使用jquery css方法设置边框样式。

<script type="text/javascript">
  $(document).ready(function(){
    $("#list2").css("border-style","solid");
  });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM