简体   繁体   English

单击时如何获取特定的文本值?

[英]How to get a specific text value when click?

{volist   name="title"  id="title"} 
    <a class="outer" href="#"><span class="inner"><b class="text">{$title.productname}</b></span></a>
{/volist}

Does anyone know how to get the text value of {$title.productname} when I click on?有谁知道我点击时如何获取 {$title.productname} 的文本值? It has six similar tags and I just want to get the one I clicked.它有六个相似的标签,我只想得到我点击的那个。

Thanks Lee谢谢李

You don't need jQuery to do this.您不需要 jQuery来执行此操作。

 $outer = document.getElementsByClassName('outer'); for(var i = 0; i < $outer.length; i++) { $outer[i].onclick = function () { console.log(this.querySelector('.text').innerHTML); } }
 <a class="outer" href="#"> <span class="inner"> <b class="text">{$title.productname}</b> </span> </a> <br/> <a class="outer" href="#"> <span class="inner"> <b class="text">{$title.productnameB}</b> </span> </a>

$('a.outer').on('click',function(){
  alert($(this).find('.text').text());
});

You've got several identifiers you could use from JQuery to get to that value: Something like:您可以从 JQuery 使用多个标识符来获取该值:例如:

$('.text').html() 

might be a good start.可能是一个好的开始。

If I understood what you need, this should work:如果我理解你需要什么,这应该有效:

$(".outer").click(function() {
    $(this).find(".text").text();
});

Edit: If these elements are generated dynamically, use Rahul Patel's suggestion.编辑:如果这些元素是动态生成的,请使用 Rahul Patel 的建议。

此选择器将专门针对包含产品名称的标签,并有助于避免与代码中的任何其他选择器发生冲突:

var boldText = $("a.outer span.inner b.text").text();

Yopu can achieve it using text() method Yopu 可以使用text()方法来实现

$("a.outer").on('click',function(){
alert($(this).text());
})

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

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