我有以下测试代码:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<DT>change me dynamically</DT>
<DD>
<input class=" text" type="text" name="contact_details" value="" id="contact_details" >
<P CLASS="descr"></P>
</DD>
<script type="text/javascript">
$(document).ready(function() {
//loop thorugh all controls. find all labels (nodeType 1. Find the "change my dynamically" text. assign ID to control for use later.)
$("*").contents().each(function() {
if(this.nodeType == 1)
this.nodeValue = this.nodeValue.replace("change me dynamically", "bingo! you found me!");
$(this).attr("id","theid");
alert(this.nodeValue);
});
});
$('#theid').click(function() {
alert('you did it!');
});
</script>
这是我正在尝试做的事情:
- 找到其中包含“动态更改我”文本的那个。
- 分配一个 ID,如“id=theid”
我假设 nodeType 是一个,因为我更改了代码,因此我确实从一开始就分配了一个 ID,然后操纵了一个 onclick 事件......就像这样:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<DT id="theid">change me dynamically</DT>
<DD>
<input class=" text" type="text" name="contact_details" value="" id="contact_details" >
<P CLASS="descr"></P>
</DD>
<script type="text/javascript">
$(document).ready(function() {
//loop thorugh all controls. find all labels (nodeType 1. Find the "change my dynamically" text. assign ID to control for use later.)
$("*").contents().each(function() {
if(this.nodeType == 1)
this.nodeValue = this.nodeValue.replace("change me dynamically", "bingo! you found me!");
$(this).attr("id","theid");
alert(this.nodeValue);
});
});
$('#theid').click(function() {
alert(this.nodeType);
});
</script>
警报命令显示“1”作为节点类型。但我显然做错了什么,因为我无法改变文本。
另一条评论 - 我知道我可以找到所有项目而不是获取所有内容......但我将对其他元素类型使用类似的逻辑,而不仅仅是 s。谢谢。
编辑 1
HTML 比我向您展示的要多得多……这是完整的 HTML:
<div id="page">
<div id="header">
<div class="leader">
<a href="#Content" class="hide">[Skip to main content]</a>
</div>
<div id="logo">
<div class="leader"></div>
<a href="/">home</a> |
<a href="">about</a>
</p></span>
<div class="tailer"></div>
</div> <!-- header -->
<div id="main">
<div class="leader">
</div>
<div id="nav">
<div class="leader">
<h3 class="hide">[Main menu]</h3>
</div>
<ul>
<li>Applications
<ul>
<li class='selected'><a class='selected' href="">Voicemail</a></li>
</ul>
</li>
</ul>
<div class="tailer">
</div>
</div> <!-- nav -->
<a name="Content"></a>
<div id="content">
<div class="leader">
</div>
<form action="" method="POST">
<input class=" hidden" type="hidden" name="redir" value="" >
<input class=" hidden" type="hidden" name="id" value="" readonly="true" >
<DT>Mobile Number</DT>
<DD>
<input class="testclass text" type="text" name="mobile_number" value="5371" id="mobile_number" readonly="false" >
</DD>
<DT>Rule Name</DT>
<DD>
<input class=" text" type="text" name="rulename" value="" >
<P CLASS="descr">Give this rule a name</P>
</DD>
<DT>Rule Type:</DT>
<DD>
<select name="ruletypes" id="ruletypes" ><option selected value="" ></option></select>
<P CLASS="descr"></P>
</DD>
<DT>When to use this rule</DT>
<DD>
<select name="rule_condition" id="rule_condition" ><option selected value="" ></option></select>
<P CLASS="descr"></P>
</DD>
<DT>What's the person's number?</DT>
<DD>
<input class=" text" type="text" name="caller_id_number" value="" id="caller_id_number" >
<P CLASS="descr"></P>
</DD>
<DT>Start Time</DT>
<DD>
<input class=" text" type="text" name="start_time" value="" id="start_time" >
<P CLASS="descr"></P>
</DD>
<DT>End Time</DT>
<DD>
<input class=" text" type="text" name="end_time" value="" id="end_time" >
<P CLASS="descr"></P>
</DD>
<input class=" hidden" type="hidden" name="user_id" value="1" readonly="true" >
<DT>Contact Type</DT>
<DD>
<select name="contact_types" id="contact_types" ><option selected value="" ></option></select>
<P CLASS="descr"></P>
</DD>
<DT>change me dynamically</DT>
<DD>
<input class=" text" type="text" name="contact_details" value="" id="contact_details" >
<P CLASS="descr"></P>
</DD>
<DT> Order</DT>
<DT></DT><DD><input class="submit" type="submit" name="submit" value="Create Rule">
</DD>
</FORM></DL>
<div class="tailer">
</div>
</div> <!-- content -->
</div> <!-- main -->
<div id="footer">
<div class="leader">
</div>
<div class="tailer">
</div>
</div> <!-- footer -->
</div> <!-- page -->