gpt4 book ai didi

Javascript:希望 'this.title' 成为 DOM 元素的标题,而不是窗口

转载 作者:行者123 更新时间:2023-12-02 22:12:48 26 4
gpt4 key购买 nike

我最近问过this question 。我获得了 a jsFiddle solution ...这适用于 jsFiddle,但不适用于我的代码。不幸的是,当我运行代码(使用 Visual Studio 2019 - .NET Core 3 网站)时,this.title value 返回页面标题中的任何值,而不是 DOM 元素。我已经在 Edge 和 Chrome 中运行了这个,结果相同。

在我的布局页面中,我有所有脚本:

<script type="text/javascript" src="~/Content/Scripts/rightClick.js"></script>
<script type="text/javascript" src="~/Content/Scripts/OpenTargetWindow.js"></script>
<script type="text/javascript" src="~/Content/Scripts/SetDefinitions.js"></script>
<script type="text/javascript" src="~/Content/Scripts/SearchApplicants.js"></script> <!--This is the script with the code in question -->

SearchApplicants.js 中的代码是:

$(function () {
$('.searchLetters').on('click', function () { addClickedLetter(this.title); });
});

function addClickedLetter(letter) {
searchString = $("#txtSearchLastName").val() + letter;
$("#txtSearchLastName").val(searchString);
};

我什至尝试将该脚本代码直接放入布局页面(而不是引用脚本),但它仍然有 this.title作为 <title></title> 的值元素,而不是我想要的链接元素。

不幸的是,该问题已关闭,我无法获得有关该问题的进一步答案或评论。该代码按照我想要的方式添加 onclick 函数,现在的问题是 this.title作为每个链接元素的标题而不是页面的标题。

更新

这是完整呈现的 HTML:

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<link href="~/Content/Styles/site.css" rel="stylesheet" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="~/Content/Scripts/rightClick.js"></script>
<script type="text/javascript" src="~/Content/Scripts/OpenTargetWindow.js"></script>
<script type="text/javascript" src="~/Content/Scripts/SetDefinitions.js"></script>
<script type="text/javascript" src="~/Content/Scripts/SearchApplicants.js"></script>
<script>
$(function () {
$(document).tooltip();
});
</script>
</head>
<body class="style1">
<img id="banner" src="~/Content/Images/Banner.jpg" alt="Need to put something here..." />
<form id="masterForm">
<div>
<table class="style1" summary="This table is for formatting purposes only.">
<tr>
<td colspan="2">
<h2>This is for a title...</h2>
<hr />
</td>
</tr>
<tr>
<td style="width:15%" valign="top">
<table summary="This table is for formatting purposes only.">
<tr>
<td style="background-color:#C0C0C0">
<a asp-controller="Search" asp-action="Index" accesskey="A"><u>A</u>liens</a>
</td>
</tr>
<tr>
<td style="background-color:#C0C0C0">
<a asp-controller="Search" asp-action="Cowboys" accesskey="C"><u>C</u>owboys</a>
</td>
</tr>
<tr>
<td style="background-color:#C0C0C0">
<a asp-controller="Search" asp-action="ET" accesskey="E"><u>E</u>.T.</a>
</td>
</tr>
<tr>
<td style="background-color:#C0C0C0">
<a asp-controller="Home" asp-action="Logout" accesskey="L"><u>L</u>ogout</a>
</td>
</tr>
</table>
</td>
<td>
<label id="lblTitle" style="font-weight:bold;">Alien Search Page</label>
<br />
<br />
<a id="addAToSearch" class="searchLetters" title="A" accesskey="A">A</a>
<a id="addBToSearch" class="searchLetters" title="B" accesskey="B">B</a>
<a id="addCToSearch" class="searchLetters" title="C" accesskey="C">C</a>
<a id="addDToSearch" class="searchLetters" title="D" accesskey="D">D</a>
<a id="addEToSearch" class="searchLetters" title="E" accesskey="E">E</a>
<a id="addFToSearch" class="searchLetters" title="F" accesskey="F">F</a>
<a id="addGToSearch" class="searchLetters" title="G" accesskey="G">G</a>
<a id="addHToSearch" class="searchLetters" title="H" accesskey="H">H</a>
<a id="addIToSearch" class="searchLetters" title="I" accesskey="I">I</a>
<a id="addJToSearch" class="searchLetters" title="J" accesskey="J">J</a>
<a id="addKToSearch" class="searchLetters" title="K" accesskey="K">K</a>
<a id="addLToSearch" class="searchLetters" title="L" accesskey="L">L</a>
<a id="addMToSearch" class="searchLetters" title="M" accesskey="M">M</a>
<a id="addNToSearch" class="searchLetters" title="N" accesskey="N">N</a>
<a id="addOToSearch" class="searchLetters" title="O" accesskey="O">O</a>
<a id="addPToSearch" class="searchLetters" title="P" accesskey="P">P</a>
<a id="addQToSearch" class="searchLetters" title="Q" accesskey="Q">Q</a>
<a id="addRToSearch" class="searchLetters" title="R" accesskey="R">R</a>
<a id="addSToSearch" class="searchLetters" title="S" accesskey="S">S</a>
<a id="addTToSearch" class="searchLetters" title="T" accesskey="T">T</a>
<a id="addUToSearch" class="searchLetters" title="U" accesskey="U">U</a>
<a id="addVToSearch" class="searchLetters" title="V" accesskey="V">V</a>
<a id="addWToSearch" class="searchLetters" title="W" accesskey="W">W</a>
<a id="addXToSearch" class="searchLetters" title="X" accesskey="X">X</a>
<a id="addYToSearch" class="searchLetters" title="Y" accesskey="Y">Y</a>
<a id="addZToSearch" class="searchLetters" title="Z" accesskey="Z">Z</a>
<br />
<label id="lblSearchLastName" for="txtSearchLastName" accesskey="N">Last Name:&nbsp;</label>
<input id="txtSearchLastName" type="text" tabindex="1" maxlength="23" class="uppercase" readonly />
<button id="btnClear" tabindex="27" title="Clear" accesskey="C">Clear</button>
<button id="btnList" tabindex="28" title="Show List" accesskey="S">Show List</button>
<hr />
</td>
</tr>
</table>
<br /><br />
<div>
Some text goes here...
</div>
<br />
</div>
</form>
</body>
</html>

这是 rightClick.js 中的内容:

//******************************************************************************
// Module : rightClick.js
//******************************************************************************

var BM = 2; // button middle
var BR = 3; // button right
var msg = "Mouse right-click is not supported on this page.";

function mouseDown(e) {
try { if (event.button == BM || event.button == BR) { return false; } }
catch (e) { if (e.which == BR) { return false; } }
}
document.oncontextmenu = function () { alert(msg); return false; }
document.ondragstart = function () { alert(msg); return false; }
document.onmousedown = mouseDown;
//******************************************************************************

以下是 OpenTargetWindow.js 中的内容:

function openTargetWindow(form, windowName) { var features; var PWfeatures; var sWidth = 778; var sHeight = 580; var PWWidth = 386; var PWHeight = 255; var PWCenterLeft = (screen.width / 2 - PWWidth / 2); var PWCenterTop = (screen.height / 2 - PWHeight / 2); var is_NN = (navigator.appName.indexOf('Netscape') != -1); var is_IE = (((navigator.userAgent.indexOf('MSIE') != -1) || (navigator.userAgent.indexOf('opera') == -1)) && (!is_NN)); if (is_IE) { features = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=0,top=20,left=14'; features += ',height=' + sHeight + ',width=' + sWidth; PWfeatures = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=0,top=' + PWCenterTop + ',left=' + PWCenterLeft + ''; PWfeatures += ',height=' + PWHeight + ',width=' + PWWidth; } else { features = 'toolbar=no,menubar=no,scrollbars=yes,location=no,border=yes,status=yes,screenX=0,top=20,left=5,autocomplete=no,frameborder=no,directories=no'; PWfeatures = 'toolbar=no,menubar=no,scrollbars=yes,location=no,border=yes,status=no,screenX=0,top=' + PWCenterTop + ',left=' + PWCenterLeft + ',autocomplete=no,frameborder=no,directories=no'; if (window.screen) { features += ',height=' + sHeight + ',width=' + sWidth; PWfeatures += ',height=' + PWHeight + ',width=' + PWWidth + ''; } else { if (window.all) { features += ',fullscreen=yes'; } else { features += ',height=480,width=640'; } } } var main = open(form, windowName, features); }

rightClick.js 和 OpenTargetWindow.js 都是从以前的应用程序复制的。

这是 SetDefinitions.js 中的内容:

$(function () {

function setDefinitionTitle(className, defintion) {
var childElements = document.getElementsByClassName(className);

for (i = 0; i < childElements.length; ++i) {
var ele = childElements[i];
ele.setAttribute('title', defintion);
}
};

function setDefinition1() {
setDefinitionTitle('def1', 'The first definition...');
}

function setDefinition2() {
setDefinitionTitle('def2', 'The second definition...');
}

function setDefinition3() {
setDefinitionTitle('def3', 'The third definition...');
}

function setDefinition4() {
setDefinitionTitle('def4', 'The fourth definition...');
}

setDefinition1();
setDefinition2();
setDefinition3();
setDefinition4();
});

最后,这是 SearchApplicants.js 中的内容:

$(function () {
$('.searchLetters').on('click', function () { addClickedLetter(this.title); });
});

function addClickedLetter(letter) {
searchString = $("#txtSearchLastName").val() + letter;
$("#txtSearchLastName").val(searchString);
};

最佳答案

也许你可以尝试这样的事情:

$(function () {
$('.searchLetters').on('click', function (e) { addClickedLetter(e.currentTarget.value); });
});

关于Javascript:希望 'this.title' 成为 DOM 元素的标题,而不是窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59504281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com