简体   繁体   English

<a>在javascript中</a>设置<a>标签</a>的title属性

[英]Setting the title attribute of an <a> tag in javascript

I'm trying to make changes in a code which shows, "title", using jQuery, from this JavaScript code:我正在尝试使用 jQuery 从此 JavaScript 代码更改显示“标题”的代码:

<a id="photo" href="{%=file.url%}" title="{%=file.name%}" 
download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>

Using this " title="{%=file.name%} " it shows all the name of the file (containing image type - .jpg.)使用这个“ title="{%=file.name%} "它显示了文件的所有名称(包含图像类型 - .jpg。)

I tried to add an id, as upper, and use:我尝试添加一个 id,作为上层,并使用:

document.getElementById("photo").setAttribute("title", "new title");

But I coudn't make it working.但我无法让它发挥作用。 I would like add to the script:我想添加到脚本中:

 var title2="{%=file.name%}";
 title = (title2.slice(2, title2.length-4));    

To cut first 2 chars and 4 last.剪切前 2 个字符和最后 4 个字符。 And then set it as a title.然后将其设置为标题。

Anyone can help?任何人都可以帮忙吗?

Thanks!: )谢谢!: )


<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}

<td style="padding-left:10px; display: inline-block;" class="template-download fade">
<div align="center" class="thumbnail" class="preview">
{% if (file.thumbnailUrl) { %}

<a id="photo" href="{%=file.url%}" title="{%=file.name%}" 
download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>           

 {% } %} </div>

<p class="name">
{% if (file.url) { %}
<a href="{%=file.url%}"  {%=file.thumbnailUrl?'data-gallery':''%}></a>

{% } %}</p></td>'{% } %}
</script>

I'am sending it couse, the < a > tag is in the JS, in the body, not in clean html, and maybe document.getElementsById, and document.getElementsByName will not work here?我发送它的原因是,< a > 标签在 JS 中,在正文中,而不是在干净的 html 中,也许 document.getElementsById 和 document.getElementsByName 在这里不起作用? And I don't know from where I shoud call this ID.我不知道我应该从哪里调用这个 ID。 There are small bug in this JS (')but it works!这个JS(')中有一个小错误,但它有效! :) :)

You can achive what you want by using this.你可以通过使用它来实现你想要的。

   var str = '';
//get name and put it in str here.
document.getElementsById("photo").title = str.slice(2, str.length-4);//str is the the name of the file.

or you can get all the links using或者你可以使用

document.getElementsByName("link")[0].title = str.slice(2, str.length-4);

where your link should have name attr您的链接应该具有name attr

<a href="#" name="link" tit......><img src="#"></a>

Hope that helps!希望有帮助!

Can you try like this你可以这样试试吗

var  image = "picture.jpg"
var tiitle= image.substr(0, image.indexOf('.')); 
var tiitle = tiitle.substr(2,tiitle.length)
document.getElementById("photo").setAttribute("title", tiitle );

fiddler Link小提琴手链接

if you're using jquery, like you said, try it like this:如果你正在使用 jquery,就像你说的那样,试试这样:

<script>
$(document).ready(function () {
    var str = $('#photo').attr('title');
    $('#photo').attr('title',str.slice(2, str.length-4));
});
</script>

Can you try giving your element a name in stead, so you can search it and then try:您可以尝试为您的元素命名,以便您可以搜索它,然后尝试:

document.getElementsByName("link")[0].title = "New title";

This may not be your final solution, but if that works, then it could be that your title attribute contains some characters it can't handle, but maybe you could tell us if there are any console errors on your existing call..这可能不是您的最终解决方案,但如果可行,那么您的 title 属性可能包含一些它无法处理的字符,但也许您可以告诉我们您现有的调用是否存在任何控制台错误。

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

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