(jQuery+;Mobile)要写入变量的div中的目标数据

(jQuery+;Mobile)要写入变量的div中的目标数据,jquery,jquery-mobile,Jquery,Jquery Mobile,是否可以在单击时在元素内找到特定数据,然后将该数据写入变量) 例如,我有一个列表,其中包含导航到“#dataviewer”的“datasource”类元素: 。。。但是我可以获取“获取此数据”并将其写入变量,以便在“#datasource”页面上使用该数据吗 非常感谢。可以通过.html()函数检索“获取此数据”。关闭以下警报: alert($("a").html()); 很明显,您可以根据页面的其余部分选择查找“a”元素 要将值存储到变量中,只需按如下方式存储输出: $(".datasour

是否可以在单击时在元素内找到特定数据,然后将该数据写入变量)

例如,我有一个列表,其中包含导航到“#dataviewer”的“datasource”类元素:

。。。但是我可以获取“获取此数据”并将其写入变量,以便在“#datasource”页面上使用该数据吗

非常感谢。

可以通过
.html()
函数检索“获取此数据”。关闭以下警报:

alert($("a").html());
很明显,您可以根据页面的其余部分选择查找“a”元素

要将值存储到变量中,只需按如下方式存储输出:

$(".datasource").click(function() {
        alert("Click!");
});
// Probably not exactly what you want when there are many "a"nchor tags on a page.
var contentsOfElement = $("a").html();
// More selective based on an ID
var contentsOfElement = $("#oneParticularID").html();
// Then you can write that text, store it, or whatever you'd like
<div data-role="page">
<ul data-role="listview">
   <li class="datasource">
      <a href="#dataviewer" >Row A</a>
   </li>
   <li class="datasource">
      <a href="#dataviewer" >Row B</a>
   </li>

</ul>
</div>​​​​​​​
​$(".datasource").click (function () {
   alert( $(this).find('a').first().html() );
});​​​​​
使用您的代码作为模板,我创建了这个


对于具有如下列表视图的
.html()

$(".datasource").click(function() {
        alert("Click!");
});
// Probably not exactly what you want when there are many "a"nchor tags on a page.
var contentsOfElement = $("a").html();
// More selective based on an ID
var contentsOfElement = $("#oneParticularID").html();
// Then you can write that text, store it, or whatever you'd like
<div data-role="page">
<ul data-role="listview">
   <li class="datasource">
      <a href="#dataviewer" >Row A</a>
   </li>
   <li class="datasource">
      <a href="#dataviewer" >Row B</a>
   </li>

</ul>
</div>​​​​​​​
​$(".datasource").click (function () {
   alert( $(this).find('a').first().html() );
});​​​​​

隐马尔可夫模型。。。它似乎不喜欢某些东西,因为当我发出警报($().html())时,它只会提醒“提交”;当我把它传递给一个变量并提醒变量时,它会说same@Squishy; 您需要更具体地使用选择器$(“a”)将捕获页面上的每个锚点。这只是一个例子,当你的页面上只有一个“a”。请参阅我编辑的答案,了解如何通过id查找元素。请确保为锚点提供id,例如
@squshy;不客气!如果您有疑问,一定要经常查看jQuery文档;它非常善于向你展示语言的能力。很高兴您的问题得到了回答does.first()只在第一次单击时获取数据,但随后不获取数据?
find('a')
返回一个数组
first()
获取数组中的第一个条目。